[Epic-ID: ODUHIGH-406][Task-ID: ODUHIGH-423]Filled ReconfigurationWithSync IE,
[o-du/l2.git] / src / 5gnrsch / sch_rach.c
index becb131..6ca545b 100644 (file)
 #include "sch.h"
 #include "sch_utils.h"
 
+SchRachRsrcRspFunc SchRachRsrcRspOpts[] =
+{
+   packSchRachRsrcRsp,      /* LC */
+   MacProcSchRachRsrcRsp,   /* TC */
+   packSchRachRsrcRsp       /* LWLC */
+};
+
 /**
  * @brief Checks if PRACH can be scheduled in current slot
  *
@@ -109,7 +116,7 @@ bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
 uint8_t schCalcPrachNumRb(SchCellCb *cell)
 {
    uint8_t tableIdx = 0;
-   uint16_t puschScs = fetchScsValue(cell->cellCfg.schInitialUlBwp.bwp.scs);
+   uint16_t puschScs = convertScsEnumValToScsVal(cell->cellCfg.schInitialUlBwp.bwp.scs);
 
    for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
    {
@@ -151,7 +158,7 @@ void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo
    if(cell == NULLP)
    {
       DU_LOG("\nERROR  --> SCH : schPrachResAlloc(): Received cellCb is null");
-      return RFAILED;
+      return;
    }
 
    /* If this slot is not a PRACH occassion, return */
@@ -184,6 +191,153 @@ void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo
    DU_LOG("\nINFO  --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
 }
 
+/**
+ * @brief Process RACH resource request for CFRA
+ *
+ * @details
+ *
+ *     Function : MacSchRachRsrcReq
+ *     
+ *     This function processes RACH resorce request 
+ *     from MAC for CFRA. It assigns a dedicated preamble
+ *     to the UE and sends the same in RACH resource
+ *     response
+ *     
+ *  @param[in]  Post structure
+ *  @param[in]  RACH resource request
+ *  @return     ROK
+ *              RFAILED
+ **/
+uint8_t MacSchRachRsrcReq(Pst *pst, SchRachRsrcReq *schRachRsrcReq)
+{
+   uint8_t      ssbIdx = 0, cfraSsbIdx = 0;
+   uint8_t      firstCFPreambleIndex = 0, lastCFPreambleIndex = 0;
+   uint16_t     cellIdx = 0;
+   uint64_t     mask = 0;
+   Pst          rspPst;
+   Inst         inst = pst->dstInst - SCH_INST_START;
+   SchCellCb    *cellCb = NULLP;
+   SchUeCb      *ueCb = NULLP;
+   SchRachRsrcRsp *rachRsrcRsp = NULLP;
+
+   DU_LOG("\nINFO  -->  SCH : Received RACH resource request for Cell ID [%d] CRNTI [%d]", \
+         schRachRsrcReq->cellId, schRachRsrcReq->crnti);
+
+ /* Fill RACH resource response to MAC */
+   SCH_ALLOC(rachRsrcRsp, sizeof(SchRachRsrcRsp));
+   if(!rachRsrcRsp)
+   {   
+      DU_LOG("\nERROR  -->  SCH : Memory allocation failed for RACH resource response");
+      return RFAILED;
+   }   
+   rachRsrcRsp->cellId = schRachRsrcReq->cellId;
+   rachRsrcRsp->crnti = schRachRsrcReq->crnti;
+   rachRsrcRsp->result = RSP_OK;
+
+   /* Fill SCH to MAC Pst structure */
+   memset(&rspPst, 0, sizeof(Pst));
+   FILL_PST_SCH_TO_MAC(rspPst, inst);
+   rspPst.event = EVENT_RACH_RESOURCE_RESPONSE_TO_MAC;
+
+   /* Fetch Cell CB */
+   for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
+   {
+      if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcReq->cellId))
+      {
+         cellCb = schCb[inst].cells[cellIdx];
+         break;
+      }
+   }
+   
+   if(cellCb)
+   {
+      /* Fetch UE CB */
+      ueCb = schGetUeCb(cellCb, schRachRsrcReq->crnti);
+      if(ueCb->crnti != schRachRsrcReq->crnti)
+      {
+         DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found" ,schRachRsrcReq->crnti);
+         rachRsrcRsp->result = RSP_NOK;
+      }
+   }
+   else
+   {
+      DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found" ,schRachRsrcReq->cellId);
+      rachRsrcRsp->result = RSP_NOK;
+   }
+
+   /* Allocate SSB resource if no failure has occurred until this step */
+   if(rachRsrcRsp->result == RSP_OK)
+   {
+      /* Find first free preamble index from the pool CF preambles 
+       * Preamble index from 0 to (numCbPreamblePerSsb-1) is used for CBRA 
+       * Preamble index from numCbPreamblePerSsb to totalNumOfRAPreamble
+       * is used for CFRA */
+      firstCFPreambleIndex = cellCb->cellCfg.schRachCfg.numCbPreamblePerSsb;
+      lastCFPreambleIndex = cellCb->cellCfg.schRachCfg.totalNumRaPreamble;
+
+      /* Allocate resource for each SSB index requested */
+      for(ssbIdx = 0; ssbIdx < schRachRsrcReq->numSsb; ssbIdx++)
+      {
+         /* Find the first CF Preamble index not dedicated to any UE currently */
+         while(firstCFPreambleIndex <= lastCFPreambleIndex)
+         {
+            mask = 1 << firstCFPreambleIndex;
+            if(cellCb->dedPreambleBitMap & mask)
+            {
+               firstCFPreambleIndex++;
+               continue;
+            }
+            else
+               break;
+         }
+
+         /* If firstCFPreambleIndex > lastCFPreambleIndex, it means all
+          * dedicated preambles are in use currently. In such a case, CBRA
+          * should be initiated. 
+          * If a dedicated preamble is found, use this for CFRA and mark it as
+          * IN-USE in the bitmap.
+          * Considering only CFRA scenario for now. */
+         if(firstCFPreambleIndex <= lastCFPreambleIndex)
+         {
+            ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx = schRachRsrcReq->ssbIdx[ssbIdx]; 
+            ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx = firstCFPreambleIndex;
+            SET_ONE_BIT(firstCFPreambleIndex, cellCb->dedPreambleBitMap);
+            cfraSsbIdx++;
+            firstCFPreambleIndex++;
+         }
+         else
+         {
+            DU_LOG("\nINFO : SCH : No dedicated preameble availble to assign to ssbIdx[%d]", schRachRsrcReq->ssbIdx[ssbIdx]);
+            /* Breaking out of for loop since no dedicated preambles are available
+             * for remaining ssbIdx too */
+            break;
+         }
+      } /* End of for */
+
+      ueCb->cfraResource.numSsb = cfraSsbIdx;
+
+      if(ueCb->cfraResource.numSsb == 0)
+      {
+         /* If numSsb is 0, it means no CFRA resource was alloacted for any of the
+          * SSB Idx, hence send a negative response */
+         rachRsrcRsp->result = RSP_NOK;
+      }
+      else
+      {   
+         /* Send ssb resource information to MAC in RACH resource response */
+         rachRsrcRsp->cfraResource.numSsb = ueCb->cfraResource.numSsb;
+         memcpy(rachRsrcRsp->cfraResource.ssbResource, ueCb->cfraResource.ssbResource, \
+            ueCb->cfraResource.numSsb * sizeof(SchCfraSsbResource));
+      }
+   } /* End of if */
+
+   /* Free RACH resource request memory allocated by MAC */
+   SCH_FREE(schRachRsrcReq, sizeof(SchRachRsrcReq));
+
+   /* Send RACH resource response to MAC */
+   return (SchRachRsrcRspOpts[rspPst.selector](&rspPst, rachRsrcRsp));
+}
+
 /**
  * @brief calculate ra-rnti function. 
  *
@@ -223,11 +377,11 @@ uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
  **/
 void createSchRaCb(uint16_t tcrnti, Inst schInst)
 {
-   uint8_t ueIdx = 0;
+   uint8_t ueId = 0;
 
-   GET_UE_IDX(tcrnti, ueIdx);
-   schCb[schInst].cells[schInst]->raCb[ueIdx -1].tcrnti = tcrnti;
-   schCb[schInst].cells[schInst]->raCb[ueIdx -1].msg4recvd = FALSE;
+   GET_UE_ID(tcrnti, ueId);
+   schCb[schInst].cells[schInst]->raCb[ueId -1].tcrnti = tcrnti;
+   schCb[schInst].cells[schInst]->raCb[ueId -1].msg4recvd = FALSE;
 }
 
 /**
@@ -350,7 +504,7 @@ RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck,
  *  @param[in]  Current timing of the cell
  *  @return  ROK
  **/
-bool schProcessRaReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
+bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
 {
    bool      k2Found = false;
    uint8_t   k0TblIdx = 0, k2TblIdx = 0;
@@ -469,7 +623,7 @@ bool schProcessRaReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
       }
 
       /* Allocate resources for msg3 */
-      msg3PuschInfo = schAllocMsg3Pusch(cell->instIdx, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time);
+      msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time);
       if(msg3PuschInfo)
       {
          /* Fill RAR info */
@@ -523,7 +677,7 @@ bool schProcessRaReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
       cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
 
       /* Create raCb at SCH */
-      createSchRaCb(cell->raReq[ueId-1]->rachInd->crnti, cell->instIdx);
+      createSchRaCb(cell->raReq[ueId-1]->rachInd->crnti, schInst);
 
       /* Remove RachInd from pending RA request list */
       SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
@@ -553,7 +707,7 @@ uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
    SchRaReq  *raReq = NULLP;
    float    slotDuration;
    uint8_t  winNumSlots;
-   uint8_t  ueIdx;
+   uint8_t  ueId;
 
    if(cell == NULLP)
    {
@@ -562,8 +716,8 @@ uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
    }
 
    /* Storing RA request in cellCb */
-   GET_UE_IDX(rachInd->crnti, ueIdx);
-   if(ueIdx <= 0)
+   GET_UE_ID(rachInd->crnti, ueId);
+   if(ueId <= 0)
    {
       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
       return RFAILED;
@@ -589,10 +743,10 @@ uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
    
    /* Adding window size to window start time to get window end time */
    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
-   cell->raReq[ueIdx -1] = raReq;
+   cell->raReq[ueId -1] = raReq;
 
    /* Adding UE Id to list of pending UEs to be scheduled */
-   addUeToBeScheduled(cell, ueIdx);
+   addUeToBeScheduled(cell, ueId);
 
    return ROK;
 }