From 5eeb25e2901e05b5478bbe93f7128e476bd4eee1 Mon Sep 17 00:00:00 2001
From: Raymond Knopp <raymond.knopp@eurecom.fr>
Date: Fri, 14 Feb 2020 09:58:36 +0100
Subject: [PATCH] integration of remaining FAPI PRACH interfaces in L1 along
 with list handling for PRACH RX requests

---
 openair1/PHY/NR_TRANSPORT/nr_dlsch_tools.c | 53 --------------------
 openair1/PHY/NR_TRANSPORT/nr_prach.c       | 34 +++++++++++++
 openair1/PHY/NR_TRANSPORT/nr_ulsch.c       | 57 ++++++++++++++++++++++
 openair1/PHY/defs_gNB.h                    | 11 +++++
 openair1/SCHED_NR/fapi_nr_l1.c             | 23 +++++++--
 openair1/SCHED_NR/fapi_nr_l1.h             |  2 +-
 openair1/SCHED_NR/phy_procedures_nr_gNB.c  | 47 ++++++------------
 7 files changed, 137 insertions(+), 90 deletions(-)

diff --git a/openair1/PHY/NR_TRANSPORT/nr_dlsch_tools.c b/openair1/PHY/NR_TRANSPORT/nr_dlsch_tools.c
index b3027280275..070f8e9ca7d 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_dlsch_tools.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_dlsch_tools.c
@@ -274,25 +274,7 @@ int16_t find_nr_dlsch(uint16_t rnti, PHY_VARS_gNB *gNB,find_type_t type) {
   return first_free_index;
 }
 
-int16_t find_nr_ulsch(uint16_t rnti, PHY_VARS_gNB *gNB,find_type_t type) {
 
-  uint16_t i;
-  int16_t first_free_index=-1;
-
-  AssertFatal(gNB!=NULL,"gNB is null\n");
-  for (i=0; i<NUMBER_OF_NR_ULSCH_MAX; i++) {
-    AssertFatal(gNB->ulsch[i]!=NULL,"gNB->ulsch[%d] is null\n",i);
-    AssertFatal(gNB->ulsch[i][0]!=NULL,"gNB->ulsch[%d][0] is null\n",i);
-    LOG_D(PHY,"searching for rnti %x : ulsch_index %d=> harq_mask %x, rnti %x, first_free_index %d\n", rnti,i,gNB->ulsch[i][0]->harq_mask,gNB->ulsch[i][0]->rnti,first_free_index);
-    if ((gNB->ulsch[i][0]->harq_mask >0) &&
-        (gNB->ulsch[i][0]->rnti==rnti))       return i;
-    else if ((gNB->ulsch[i][0]->harq_mask == 0) && (first_free_index==-1)) first_free_index=i;
-  }
-  if (type == SEARCH_EXIST) return -1;
-  if (first_free_index != -1)
-    gNB->ulsch[first_free_index][0]->rnti = 0;
-  return first_free_index;
-}
 
 void nr_fill_dlsch(PHY_VARS_gNB *gNB,
                    int frame,
@@ -316,40 +298,5 @@ void nr_fill_dlsch(PHY_VARS_gNB *gNB,
 
 }
 
-void nr_fill_ulsch(PHY_VARS_gNB *gNB,
-                   int frame,
-                   int slot,
-                   nfapi_nr_pusch_pdu_t *ulsch_pdu) {
-
- 
-  int ulsch_id = find_nr_ulsch(ulsch_pdu->rnti,gNB,SEARCH_EXIST_OR_FREE);
-  AssertFatal( (ulsch_id>=0) && (ulsch_id<NUMBER_OF_NR_ULSCH_MAX),
-              "illegal or no ulsch_id found!!! rnti %04x ulsch_id %d\n",ulsch_pdu->rnti,ulsch_id);
-
-  NR_gNB_ULSCH_t  *ulsch = gNB->ulsch[ulsch_id][0];
-  int harq_pid = ulsch_pdu->pusch_data.harq_process_id;
-  ulsch->rnti = ulsch_pdu->rnti;
-  //ulsch->rnti_type;
-  ulsch->harq_mask |= 1<<harq_pid;
-  ulsch->harq_process_id[slot] = harq_pid;
 
-  nfapi_nr_ul_config_ulsch_pdu *rel15_ul = &ulsch->harq_processes[harq_pid]->ulsch_pdu;
-
-  LOG_D(PHY,"Initializing nFAPI for ULSCH, UE %d, harq_pid %d\n",ulsch_id,harq_pid);
- 
-  
-  //FK this is still a bad hack. We need to replace the L1 FAPI structures with the new scf ones as well.
-  rel15_ul->rnti                           = ulsch_pdu->rnti;
-  rel15_ul->ulsch_pdu_rel15.start_rb       = ulsch_pdu->rb_start;
-  rel15_ul->ulsch_pdu_rel15.number_rbs     = ulsch_pdu->rb_size;
-  rel15_ul->ulsch_pdu_rel15.start_symbol   = ulsch_pdu->start_symbol_index;
-  rel15_ul->ulsch_pdu_rel15.number_symbols = ulsch_pdu->nr_of_symbols;
-  rel15_ul->ulsch_pdu_rel15.length_dmrs    = gNB->dmrs_UplinkConfig.pusch_maxLength;
-  rel15_ul->ulsch_pdu_rel15.Qm             = ulsch_pdu->qam_mod_order;
-  rel15_ul->ulsch_pdu_rel15.mcs            = ulsch_pdu->mcs_index;
-  rel15_ul->ulsch_pdu_rel15.rv             = ulsch_pdu->pusch_data.rv_index;
-  rel15_ul->ulsch_pdu_rel15.n_layers       = ulsch_pdu->nrOfLayers;
-  rel15_ul->ulsch_pdu_rel15.R              = ulsch_pdu->target_code_rate;
-
-}
 
diff --git a/openair1/PHY/NR_TRANSPORT/nr_prach.c b/openair1/PHY/NR_TRANSPORT/nr_prach.c
index 394871c6310..db1452d9703 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_prach.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_prach.c
@@ -46,6 +46,40 @@ extern uint16_t prach_root_sequence_map_abc[138];
 extern uint16_t nr_du[838];
 extern int16_t nr_ru[2*839];
 
+int16_t find_nr_prach(PHY_VARS_gNB *gNB,int frame,int slot, int numRA, find_type_t type) {
+
+  uint16_t i;
+  int16_t first_free_index=-1;
+
+  AssertFatal(gNB!=NULL,"gNB is null\n");
+  for (i=0; i<NUMBER_OF_NR_PRACH_MAX; i++) {
+    LOG_D(PHY,"searching for PRACH in %d.%d with numRA %d: prach_index %d=> %d.%d numRA %d\n", frame,slot,numRA,i,
+	  gNB->prach_vars.list[i].frame,gNB->prach_vars.list[i].slot,gNB->prach_vars.list[i].pdu.num_ra);
+    if ((gNB->prach_vars.list[i].frame == frame) &&
+        (gNB->prach_vars.list[i].slot  == slot) &&
+	(gNB->prach_vars.list[i].pdu.num_ra == numRA))       return i;
+    else if ((gNB->prach_vars.list[i].frame == -1) && (first_free_index==-1)) first_free_index=i;
+  }
+  if (type == SEARCH_EXIST) return -1;
+
+  return first_free_index;
+}
+
+void nr_fill_prach(PHY_VARS_gNB *gNB,
+		   int SFN,
+		   int Slot,
+		   nfapi_nr_prach_pdu_t *prach_pdu) {
+
+  int prach_id = find_nr_prach(gNB,SFN,Slot,prach_pdu->num_ra,SEARCH_EXIST);
+  AssertFatal( (prach_id>=0) && (prach_id<NUMBER_OF_NR_PRACH_MAX),
+              "illegal or no prach_id found!!! numRA %d dlsch_id %d\n",prach_pdu->num_ra,prach_id);
+
+  gNB->prach_vars.list[prach_id].frame=SFN;
+  gNB->prach_vars.list[prach_id].slot=Slot;
+  memcpy((void*)&gNB->prach_vars.list[prach_id].pdu,(void*)prach_pdu,sizeof(*prach_pdu));
+
+}
+
 void rx_nr_prach_ru(RU_t *ru,
 		    nfapi_nr_prach_pdu_t *prach_pdu,
 		    int frame,
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
index 2bc623a66c2..f79377422fd 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
@@ -35,6 +35,63 @@
 #include "PHY/NR_TRANSPORT/nr_ulsch.h"
 #include "PHY/LTE_REFSIG/lte_refsig.h"
 
+int16_t find_nr_ulsch(uint16_t rnti, PHY_VARS_gNB *gNB,find_type_t type) {
+
+  uint16_t i;
+  int16_t first_free_index=-1;
+
+  AssertFatal(gNB!=NULL,"gNB is null\n");
+  for (i=0; i<NUMBER_OF_NR_ULSCH_MAX; i++) {
+    AssertFatal(gNB->ulsch[i]!=NULL,"gNB->ulsch[%d] is null\n",i);
+    AssertFatal(gNB->ulsch[i][0]!=NULL,"gNB->ulsch[%d][0] is null\n",i);
+    LOG_D(PHY,"searching for rnti %x : ulsch_index %d=> harq_mask %x, rnti %x, first_free_index %d\n", rnti,i,gNB->ulsch[i][0]->harq_mask,gNB->ulsch[i][0]->rnti,first_free_index);
+    if ((gNB->ulsch[i][0]->harq_mask >0) &&
+        (gNB->ulsch[i][0]->rnti==rnti))       return i;
+    else if ((gNB->ulsch[i][0]->harq_mask == 0) && (first_free_index==-1)) first_free_index=i;
+  }
+  if (type == SEARCH_EXIST) return -1;
+  if (first_free_index != -1)
+    gNB->ulsch[first_free_index][0]->rnti = 0;
+  return first_free_index;
+}
+
+void nr_fill_ulsch(PHY_VARS_gNB *gNB,
+                   int frame,
+                   int slot,
+                   nfapi_nr_pusch_pdu_t *ulsch_pdu) {
+
+ 
+  int ulsch_id = find_nr_ulsch(ulsch_pdu->rnti,gNB,SEARCH_EXIST_OR_FREE);
+  AssertFatal( (ulsch_id>=0) && (ulsch_id<NUMBER_OF_NR_ULSCH_MAX),
+              "illegal or no ulsch_id found!!! rnti %04x ulsch_id %d\n",ulsch_pdu->rnti,ulsch_id);
+
+  NR_gNB_ULSCH_t  *ulsch = gNB->ulsch[ulsch_id][0];
+  int harq_pid = ulsch_pdu->pusch_data.harq_process_id;
+  ulsch->rnti = ulsch_pdu->rnti;
+  //ulsch->rnti_type;
+  ulsch->harq_mask |= 1<<harq_pid;
+  ulsch->harq_process_id[slot] = harq_pid;
+
+  nfapi_nr_ul_config_ulsch_pdu *rel15_ul = &ulsch->harq_processes[harq_pid]->ulsch_pdu;
+
+  LOG_D(PHY,"Initializing nFAPI for ULSCH, UE %d, harq_pid %d\n",ulsch_id,harq_pid);
+ 
+  
+  //FK this is still a bad hack. We need to replace the L1 FAPI structures with the new scf ones as well.
+  rel15_ul->rnti                           = ulsch_pdu->rnti;
+  rel15_ul->ulsch_pdu_rel15.start_rb       = ulsch_pdu->rb_start;
+  rel15_ul->ulsch_pdu_rel15.number_rbs     = ulsch_pdu->rb_size;
+  rel15_ul->ulsch_pdu_rel15.start_symbol   = ulsch_pdu->start_symbol_index;
+  rel15_ul->ulsch_pdu_rel15.number_symbols = ulsch_pdu->nr_of_symbols;
+  rel15_ul->ulsch_pdu_rel15.length_dmrs    = gNB->dmrs_UplinkConfig.pusch_maxLength;
+  rel15_ul->ulsch_pdu_rel15.Qm             = ulsch_pdu->qam_mod_order;
+  rel15_ul->ulsch_pdu_rel15.mcs            = ulsch_pdu->mcs_index;
+  rel15_ul->ulsch_pdu_rel15.rv             = ulsch_pdu->pusch_data.rv_index;
+  rel15_ul->ulsch_pdu_rel15.n_layers       = ulsch_pdu->nrOfLayers;
+  rel15_ul->ulsch_pdu_rel15.R              = ulsch_pdu->target_code_rate;
+
+}
+
 void nr_ulsch_unscrambling(int16_t* llr,
                          uint32_t size,
                          uint8_t q,
diff --git a/openair1/PHY/defs_gNB.h b/openair1/PHY/defs_gNB.h
index d7dde57a117..4a78782f78c 100644
--- a/openair1/PHY/defs_gNB.h
+++ b/openair1/PHY/defs_gNB.h
@@ -43,6 +43,8 @@
 #include "PHY/CODING/nrLDPC_decoder/nrLDPC_decoder.h"
 #include "PHY/CODING/nrLDPC_decoder/nrLDPC_types.h"
 
+#include "nfapi_nr_interface_scf.h"
+
 #define MAX_NUM_RU_PER_gNB MAX_NUM_RU_PER_eNB
 
 typedef struct {
@@ -148,6 +150,14 @@ typedef struct {
   int16_t sqrt_rho_b;
 } NR_gNB_DLSCH_t;
 
+typedef struct {
+  int frame;
+  int slot;
+  nfapi_nr_prach_pdu_t pdu;  
+} gNB_PRACH_list_t;
+
+#define NUMBER_OF_NR_PRACH_MAX 8
+
 typedef struct {
   /// \brief ?.
   /// first index: ? [0..1023] (hard coded)
@@ -158,6 +168,7 @@ typedef struct {
   int16_t **rxsigF;
   /// \brief local buffer to compute prach_ifft
   int32_t *prach_ifft;
+  gNB_PRACH_list_t list[NUMBER_OF_NR_PRACH_MAX];
 } NR_gNB_PRACH;
 
 typedef struct {
diff --git a/openair1/SCHED_NR/fapi_nr_l1.c b/openair1/SCHED_NR/fapi_nr_l1.c
index 5c145a32302..2d58cadb106 100644
--- a/openair1/SCHED_NR/fapi_nr_l1.c
+++ b/openair1/SCHED_NR/fapi_nr_l1.c
@@ -145,10 +145,8 @@ void nr_schedule_response(NR_Sched_Rsp_t *Sched_INFO){
   gNB         = RC.gNB[Mod_id];
 
   uint8_t number_dl_pdu             = DL_req->dl_tti_request_body.nPDUs;
-  //  uint8_t number_ul_pdu             = 0;
   uint8_t number_ul_dci_pdu         = (UL_dci_req==NULL) ? 0 : UL_dci_req->numPdus;
-
-  //  if (UL_tti_req != NULL) number_ul_pdu = UL_tti_req->n_pdus;
+  uint8_t number_ul_tti_pdu         = (UL_tti_req==NULL) ? 0 : UL_tti_req->n_pdus;
 
   LOG_D(PHY,"NFAPI: Sched_INFO:SFN/SLOT:%04d%d DL_req:SFN/SLO:%04d%d:dl_pdu:%d tx_req:SFN/SLOT:%04d%d:pdus:%d;\n",
         frame,slot,
@@ -194,7 +192,7 @@ void nr_schedule_response(NR_Sched_Rsp_t *Sched_INFO){
     }
   }
 
-  if (UL_tti_req!=NULL) memcpy(&gNB->UL_tti_req,UL_tti_req,sizeof(nfapi_nr_ul_tti_request_t));
+  //  if (UL_tti_req!=NULL) memcpy(&gNB->UL_tti_req,UL_tti_req,sizeof(nfapi_nr_ul_tti_request_t));
   
   for (int i=0;i<number_ul_dci_pdu;i++) {
     handle_nfapi_nr_ul_dci_pdu(gNB,
@@ -202,5 +200,22 @@ void nr_schedule_response(NR_Sched_Rsp_t *Sched_INFO){
 			      &UL_dci_req->ul_dci_pdu_list[i]);
   }
 
+  for (int i = 0; i < number_ul_tti_pdu; i++) {
+    switch (UL_tti_req->pdus_list[i].pdu_type) {
+      case NFAPI_NR_UL_CONFIG_PUSCH_PDU_TYPE:
+        LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PUSCH_PDU_TYPE for %d.%d\n",frame,slot,UL_tti_req->SFN,UL_tti_req->Slot);
+        nr_fill_ulsch(gNB,frame,slot,&UL_tti_req->pdus_list[i].pusch_pdu);
+        break;
+      case NFAPI_NR_UL_CONFIG_PUCCH_PDU_TYPE:
+	LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PUCCH_PDU_TYPE for %d.%d\n",frame,slot,UL_tti_req->SFN,UL_tti_req->Slot);
+	//        handle_nfapi_nr_pucch_pdu(gNB,frame,slot,UL_tti_req->pdus_list[i].pucch_pdu);
+        break;
+      case NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE:
+	LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE for %d.%d\n",frame,slot,UL_tti_req->SFN,UL_tti_req->Slot);
+        nr_fill_prach(gNB,UL_tti_req->SFN,UL_tti_req->Slot,UL_tti_req->pdus_list[i].prach_pdu);
+        break;
+    }
+  }
+
 
 }
diff --git a/openair1/SCHED_NR/fapi_nr_l1.h b/openair1/SCHED_NR/fapi_nr_l1.h
index 5a55167c125..07509b3bb7b 100644
--- a/openair1/SCHED_NR/fapi_nr_l1.h
+++ b/openair1/SCHED_NR/fapi_nr_l1.h
@@ -48,4 +48,4 @@ void handle_nr_nfapi_pdsch_pdu(PHY_VARS_gNB *gNB,int frame,int slot,
 
 void nr_fill_rx_indication(PHY_VARS_gNB *gNB, int frame, int slot_rx, int UE_id, uint8_t harq_pid);
 
-void nr_fill_crc_indication(PHY_VARS_gNB *gNB, int frame, int slot_rx, int UE_id, uint8_t crc_flag);
\ No newline at end of file
+void nr_fill_crc_indication(PHY_VARS_gNB *gNB, int frame, int slot_rx, int UE_id, uint8_t crc_flag);
diff --git a/openair1/SCHED_NR/phy_procedures_nr_gNB.c b/openair1/SCHED_NR/phy_procedures_nr_gNB.c
index 5cbe2816fd6..8fbe7c8ba8b 100644
--- a/openair1/SCHED_NR/phy_procedures_nr_gNB.c
+++ b/openair1/SCHED_NR/phy_procedures_nr_gNB.c
@@ -390,41 +390,24 @@ void phy_procedures_gNB_common_RX(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx)
 
 void phy_procedures_gNB_uespec_RX(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx) {
 
-  nfapi_nr_ul_tti_request_t     *UL_tti_req  = &gNB->UL_tti_req;
-  int num_pdus = UL_tti_req->n_pdus;
+  LOG_D(PHY,"phy_procedures_gNB_uespec_RX frame %d, slot %d\n",frame_rx,slot_rx);
 
-  LOG_D(PHY,"phy_procedures_gNB_uespec_RX frame %d, slot %d, num_pdus %d\n",frame_rx,slot_rx,num_pdus);
+  for (int ULSCH_id=0;ULSCH_id<NUMBER_OF_NR_ULSCH_MAX;ULSCH_id++) {
+    NR_gNB_ULSCH_t *ulsch = gNB->ulsch[ULSCH_id];
 
-  gNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus  = 0;
-
-  for (int i = 0; i < num_pdus; i++) {
-    switch (UL_tti_req->pdus_list[i].pdu_type) {
-      case NFAPI_NR_UL_CONFIG_PUSCH_PDU_TYPE:
-        LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PUSCH_PDU_TYPE\n",frame_rx,slot_rx);
-
-        nfapi_nr_pusch_pdu_t  *pusch_pdu = &UL_tti_req->pdus_list[0].pusch_pdu;
-        nr_fill_ulsch(gNB,frame_rx,slot_rx,pusch_pdu);
-
-        uint8_t ULSCH_id =  find_nr_ulsch(pusch_pdu->rnti,gNB,SEARCH_EXIST);
-        uint8_t harq_pid = pusch_pdu->pusch_data.harq_process_id;
-        uint8_t symbol_start = pusch_pdu->start_symbol_index;
-        uint8_t symbol_end = symbol_start + pusch_pdu->nr_of_symbols;
+    if (ulsch->rnti > 0) {
+      uint8_t harq_pid = ulsch->harq_process_id[slot_rx];
+      uint8_t symbol_start = ulsch->harq_processes[harq_pid]->ulsch_pdu.ulsch_pdu_rel15.start_symbol;
+      uint8_t symbol_end = symbol_start + ulsch->harq_processes[harq_pid]->ulsch_pdu.ulsch_pdu_rel15.number_symbols;
       
-        for(uint8_t symbol = symbol_start; symbol < symbol_end; symbol++) {
-          nr_rx_pusch(gNB, ULSCH_id, frame_rx, slot_rx, symbol, harq_pid);
-        }
-        //LOG_M("rxdataF_comp.m","rxF_comp",gNB->pusch_vars[0]->rxdataF_comp[0],6900,1,1);
-        //LOG_M("rxdataF_ext.m","rxF_ext",gNB->pusch_vars[0]->rxdataF_ext[0],6900,1,1);
-        nr_ulsch_procedures(gNB, frame_rx, slot_rx, ULSCH_id, harq_pid);
-        nr_fill_rx_indication(gNB, frame_rx, slot_rx, ULSCH_id, harq_pid);  // indicate SDU to MAC
-        nr_fill_crc_indication(gNB, frame_rx, slot_rx, ULSCH_id, 0);
-        break;
-      case NFAPI_NR_UL_CONFIG_PUCCH_PDU_TYPE:
-	LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PUCCH_PDU_TYPE\n",frame_rx,slot_rx);
-        break;
-      case NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE:
-	LOG_D(PHY,"frame %d, slot %d, Got NFAPI_NR_UL_CONFIG_PRACH_PDU_TYPE\n",frame_rx,slot_rx);
-        break;
+      for(uint8_t symbol = symbol_start; symbol < symbol_end; symbol++) {
+	nr_rx_pusch(gNB, ULSCH_id, frame_rx, slot_rx, symbol, harq_pid);
+      }
+      //LOG_M("rxdataF_comp.m","rxF_comp",gNB->pusch_vars[0]->rxdataF_comp[0],6900,1,1);
+      //LOG_M("rxdataF_ext.m","rxF_ext",gNB->pusch_vars[0]->rxdataF_ext[0],6900,1,1);
+      nr_ulsch_procedures(gNB, frame_rx, slot_rx, ULSCH_id, harq_pid);
+      nr_fill_rx_indication(gNB, frame_rx, slot_rx, ULSCH_id, harq_pid);  // indicate SDU to MAC
+      nr_fill_crc_indication(gNB, frame_rx, slot_rx, ULSCH_id, 0);
     }
   }
 }
-- 
GitLab