[Epic-ID: ODUHIGH-516][Task-ID: ODUHIGH-523] Statistics Response between DU APP and...
[o-du/l2.git] / src / 5gnrsch / sch.c
index 7edd709..8e90431 100644 (file)
 #include "rg_sch_inf.x"         /* typedefs for Scheduler */
 #include "mac_sch_interface.h"
 #include "sch.h"
+#include "sch_tmr.h"
 #include "sch_utils.h"
 #include "sch_fcfs.h"
 #include "sch_slice_based.h"
 
-/**
- * @brief Task Initiation function. 
- *
- * @details
- *
- *     Function : schActvInit
- *     
- *     This function is supplied as one of parameters during MAC's 
- *     task registration. MAC will invoke this function once, after
- *     it creates and attaches this TAPA Task to a system task.
- *     
- *  @param[in]  Ent Entity, the entity ID of this task.     
- *  @param[in]  Inst Inst, the instance ID of this task.
- *  @param[in]  Region Region, the region ID registered for memory 
- *              usage of this task.
- *  @param[in]  Reason Reason.
- *  @return  int
- *      -# ROK
- **/
-uint8_t schActvInit(Ent entity, Inst instId, Region region, Reason reason)
-{
-   Inst inst = (instId  - SCH_INST_START);
-
-   /* Initialize the MAC TskInit structure to zero */
-   memset ((uint8_t *)&schCb[inst], 0, sizeof(schCb));
-
-   /* Initialize the MAC TskInit with received values */
-   schCb[inst].schInit.ent = entity;
-   schCb[inst].schInit.inst = inst;
-   schCb[inst].schInit.region = region;
-   schCb[inst].schInit.pool = 0;
-   schCb[inst].schInit.reason = reason;
-   schCb[inst].schInit.cfgDone = FALSE;
-   schCb[inst].schInit.acnt = FALSE;
-   schCb[inst].schInit.usta = FALSE;
-   schCb[inst].schInit.trc = FALSE;
-   schCb[inst].schInit.procId = ODU_GET_PROCID();
-
-   return ROK;
-} /* schActvInit */
-
 /**
  * @brief Scheduler All Apis initialized. 
  *
@@ -150,28 +110,27 @@ uint8_t SchInstCfg(RgCfg *cfg, Inst  dInst)
 
    schCb[inst].schInit.region = cfg->s.schInstCfg.genCfg.mem.region;
    schCb[inst].schInit.pool = cfg->s.schInstCfg.genCfg.mem.pool;
-   schCb[inst].genCfg.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
 #ifdef LTE_ADV
    schCb[inst].genCfg.forceCntrlSrbBoOnPCel =  cfg->s.schInstCfg.genCfg.forceCntrlSrbBoOnPCel;
    schCb[inst].genCfg.isSCellActDeactAlgoEnable =  cfg->s.schInstCfg.genCfg.isSCellActDeactAlgoEnable;
 #endif
    schCb[inst].genCfg.startCellId    = cfg->s.schInstCfg.genCfg.startCellId;
 
+   schCb[inst].schTimersInfo.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
    /* Initialzie the timer queue */   
-   memset(&schCb[inst].tmrTq, 0, sizeof(CmTqType) * SCH_TQ_SIZE);
+   memset(&schCb[inst].schTimersInfo.tmrTq, 0, sizeof(CmTqType) * SCH_TQ_SIZE);
    /* Initialize the timer control point */
-   memset(&schCb[inst].tmrTqCp, 0, sizeof(CmTqCp));
-   schCb[inst].tmrTqCp.tmrLen = RGSCH_TQ_SIZE;
+   memset(&schCb[inst].schTimersInfo.tmrTqCp, 0, sizeof(CmTqCp));
+   schCb[inst].schTimersInfo.tmrTqCp.tmrLen = SCH_TQ_SIZE;
 
    /* SS_MT_TMR needs to be enabled as schActvTmr needs instance information */
    /* Timer Registration request to system services */
-   if (ODU_REG_TMR_MT(schCb[inst].schInit.ent, dInst, (int)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
+   if (ODU_REG_TMR_MT(schCb[inst].schInit.ent, dInst, (int)schCb[inst].schTimersInfo.tmrRes, schActvTmr) != ROK)
    {
-      DU_LOG("\nERROR  -->  SCH : SchInstCfg(): Failed to "
-           "register timer.");
+      DU_LOG("\nERROR  -->  SCH : SchInstCfg(): Failed to register timer.");
       return (LCM_REASON_MEM_NOAVAIL);
    }   
-              
+
    /* Set Config done in TskInit */
    schCb[inst].schInit.cfgDone = TRUE;
    DU_LOG("\nINFO   -->  SCH : Scheduler gen config done");
@@ -381,7 +340,7 @@ void schInitTddSlotCfg(SchCellCb *cell, SchCellCfg *schCellCfg)
    int8_t slotIdx, symbIdx;
 
    periodicityInMicroSec = schGetPeriodicityInMsec(schCellCfg->tddCfg.tddPeriod);
-   cell->numSlotsInPeriodicity = (periodicityInMicroSec * pow(2, schCellCfg->numerology))/1000;
+   cell->numSlotsInPeriodicity = (periodicityInMicroSec * pow(2, cell->numerology))/1000;
    cell->slotFrmtBitMap = 0;
    schFillSlotConfig(cell, schCellCfg->tddCfg);
    for(slotIdx = cell->numSlotsInPeriodicity-1; slotIdx >= 0; slotIdx--)
@@ -434,7 +393,7 @@ void fillSsbStartSymb(SchCellCb *cellCb)
 {
    uint8_t cnt, scs, symbIdx, ssbStartSymbArr[SCH_MAX_SSB_BEAM];
 
-   scs = cellCb->cellCfg.scsCommon;
+   scs = cellCb->cellCfg.ssbScs;
 
    memset(ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
    symbIdx = 0;
@@ -496,6 +455,7 @@ void fillSsbStartSymb(SchCellCb *cellCb)
  **/
 uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
 {
+   uint16_t scsInKhz = 0;
    SchCellCb *cell= NULLP;
    SCH_ALLOC(cell, sizeof(SchCellCb));
    if(!cell)
@@ -506,7 +466,11 @@ uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
 
    cell->cellId = schCellCfg->cellId; 
    cell->instIdx = inst;
-   switch(schCellCfg->numerology)
+   scsInKhz = convertScsEnumValToScsVal(schCellCfg->ssbScs);
+   
+   /*Ref : 3GPP 38.211 Table 4.2-1: SCS = (2 ^ numerology * 15kHz)*/
+   cell->numerology = log2(scsInKhz/BASE_SCS);
+   switch(cell->numerology)
    {
       case SCH_NUMEROLOGY_0:
         {
@@ -534,7 +498,7 @@ uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
         }
         break;
       default:
-        DU_LOG("\nERROR  -->  SCH : Numerology %d not supported", schCellCfg->numerology);
+        DU_LOG("\nERROR  -->  SCH : Numerology %d not supported", cell->numerology);
    }
 #ifdef NR_TDD
    schInitTddSlotCfg(cell, schCellCfg);   
@@ -795,7 +759,7 @@ uint8_t SchProcCellCfgReq(Pst *pst, SchCellCfg *schCellCfg)
    cellCb->macInst = pst->srcInst;
 
    /* derive the SIB1 config parameters */
-   ret = fillSchSib1Cfg(schCellCfg->numerology, schCellCfg->dlBandwidth, cellCb->numSlots,
+   ret = fillSchSib1Cfg(cellCb->numerology, schCellCfg->dlBandwidth, cellCb->numSlots,
         &(schCellCfg->pdcchCfgSib1), &(cellCb->sib1SchCfg), schCellCfg->phyCellId,
         schCellCfg->dlCfgCommon.schFreqInfoDlSib.offsetToPointA, schCellCfg->sib1PduLen);
    
@@ -1417,6 +1381,10 @@ uint8_t allocatePrbDl(SchCellCb *cell, SlotTimingInfo slotTime, \
          return RFAILED;
       }
    }
+   
+   /* Update statistics of PRB usage if stats calculation is enabled */
+   if(schCb[cell->instIdx].statistics.dlTotalPrbUsage)
+      prbAlloc->numPrbAlloc += numPrb;
 
    /* Update the remaining number for free PRBs */
    removeAllocatedPrbFromFreePrbList(&prbAlloc->freePrbBlockList, freePrbNode, *startPrb, numPrb);
@@ -1551,6 +1519,10 @@ uint8_t allocatePrbUl(SchCellCb *cell, SlotTimingInfo slotTime, \
       }
    }
 
+   /* Update statistics of PRB usage if stats calculation is enabled */
+   if(schCb[cell->instIdx].statistics.ulTotalPrbUsage)
+      prbAlloc->numPrbAlloc += numPrb;
+
    /* Update the remaining number for free PRBs */
    removeAllocatedPrbFromFreePrbList(&prbAlloc->freePrbBlockList, freePrbNode, *startPrb, numPrb);
 
@@ -1797,12 +1769,12 @@ uint8_t fillSliceCfgRsp(Inst inst, CmLListCp *storedSliceCfg, SchCellCb *cellCb,
                if(addSliceCfgInSchDb(storedSliceCfg, schSliceCfgReq->listOfSlices[cfgIdx]) == ROK)
                {
                   sliceFound = RSP_OK;
-                  schSliceCfgRsp.cause = SLICE_CONFIGURED;
+                  schSliceCfgRsp.cause = SUCCESSFUL;
                }
                else
                {
                   DU_LOG("\nERROR  --> SCH : Failed to store slice configuration in SchDb");
-                  schSliceCfgRsp.cause = RESOURCE_NOT_AVAILABLE;
+                  schSliceCfgRsp.cause = RESOURCE_UNAVAILABLE;
                   ret = RFAILED;
                }
                plmnIdx = MAX_PLMN;
@@ -1811,7 +1783,7 @@ uint8_t fillSliceCfgRsp(Inst inst, CmLListCp *storedSliceCfg, SchCellCb *cellCb,
          }
       }
       
-      if((sliceFound == RSP_NOK) && (schSliceCfgRsp.cause != RESOURCE_NOT_AVAILABLE))
+      if((sliceFound == RSP_NOK) && (schSliceCfgRsp.cause != RESOURCE_UNAVAILABLE))
          schSliceCfgRsp.cause = SLICE_NOT_FOUND;
       
       schSliceCfgRsp.snssai = schSliceCfgReq->listOfSlices[cfgIdx]->snssai;
@@ -1977,7 +1949,7 @@ uint8_t fillSliceRecfgRsp(Inst inst, CmLListCp *storedSliceCfg, SchSliceRecfgReq
       schSliceRecfgRsp.snssai = schSliceRecfgReq->listOfSlices[cfgIdx]->snssai;
       schSliceRecfgRsp.rsp    = sliceFound;
       if(schSliceRecfgRsp.rsp == RSP_OK)
-         schSliceRecfgRsp.cause = SLICE_RECONFIGURED;
+         schSliceRecfgRsp.cause = SUCCESSFUL;
       else
          schSliceRecfgRsp.cause = SLICE_NOT_FOUND;
       SchSendSliceRecfgRspToMac(inst, schSliceRecfgRsp);
@@ -2529,6 +2501,195 @@ uint8_t SchProcPhrInd(Pst *pst, SchPwrHeadroomInd *schPhrInd)
    return ret;
 }
 
+/*******************************************************************
+ *
+ * @brief Fill and send statistics response to MAC
+ *
+ * @details
+ *
+ *    Function :  SchSendStatsRspToMac
+ *
+ *    Functionality: Fill and send statistics response to MAC
+ *
+ * @params[in]  Inst inst, SchMacRsp result
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+uint8_t SchSendStatsRspToMac(Inst inst, SchMacRsp result, CauseOfResult cause)
+{
+   Pst rspPst;
+   uint8_t ret = ROK;
+   SchStatsRsp  *statsRsp;
+
+   DU_LOG("\nINFO   --> SCH : Filling statistics response");
+   SCH_ALLOC(statsRsp, sizeof(SchStatsRsp));
+   if(statsRsp == NULLP)
+   {
+      DU_LOG("\nERROR  --> SCH : Failed to allocate memory in SchSendStatsRspToMac()");
+      return RFAILED;
+   }
+   memset(statsRsp, 0, sizeof(SchStatsRsp));
+   statsRsp->rsp = result;
+   statsRsp->cause = cause;
+
+   /* Filling response post */
+   memset(&rspPst, 0, sizeof(Pst));
+   FILL_PST_SCH_TO_MAC(rspPst, inst);
+   rspPst.event = EVENT_STATISTICS_RSP_TO_MAC;
+
+   ret = MacMessageRouter(&rspPst, (void *)statsRsp);
+   if(ret == RFAILED)
+   {
+      DU_LOG("\nERROR  -->  SCH : SchSendStatsRspToMac(): Failed to send Statistics Response");
+      return ret;
+   }
+   return ret;
+}
+
+/*******************************************************************
+ *
+ * @brief Processes Statistics Request from MAC
+ *
+ * @details
+ *
+ *    Function : SchProcStatsReq
+ *
+ *    Functionality:
+ *       Processes Statistics Request from MAC
+ *
+ * @params[in] 
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+uint8_t SchProcStatsReq(Pst *pst, SchStatsReq *statsReq)
+{
+   uint8_t idx;
+   Inst    inst = pst->dstInst - SCH_INST_START;
+   SchMacRsp     rsp = RSP_OK;
+   CauseOfResult cause = SUCCESSFUL;
+   bool isDlTotlPrbUseCfgd = false, isUlTotlPrbUseCfgd = false;
+
+   DU_LOG("\nINFO   -->  SCH : Received Statistics Request from MAC");
+
+   for(idx=0; idx < statsReq->numStats; idx++)
+   {
+      switch(statsReq->statsList[idx].type)
+      {
+         case SCH_DL_TOTAL_PRB_USAGE:
+            {
+               /* Check if duplicate configuration */
+               if(schCb[inst].statistics.dlTotalPrbUsage)
+               {
+                  DU_LOG("\nERROR   -->  SCH : SCH_DL_TOTAL_PRB_USAGE stats already configured");
+                  rsp = RSP_NOK;
+                  cause = DUPLICATE_ENTRY;
+               }
+
+               /* Allocate memory */
+               SCH_ALLOC(schCb[inst].statistics.dlTotalPrbUsage, sizeof(TotalPrbUsage));
+               if(!schCb[inst].statistics.dlTotalPrbUsage)
+               {
+                  DU_LOG("\nERROR   -->  SCH : Memory allocation failed for dlTotalPrbUsage in \
+                        SchProcStatsReq()");
+                  rsp = RSP_NOK;
+                  cause = RESOURCE_UNAVAILABLE;
+                  break;
+               }
+
+               /* Initialize */
+               memset(schCb[inst].statistics.dlTotalPrbUsage, 0, sizeof(TotalPrbUsage));
+
+               /* Configure */
+               schCb[inst].statistics.dlTotalPrbUsage->schInst = inst;
+               schCb[inst].statistics.dlTotalPrbUsage->periodicity = statsReq->statsList[idx].periodicity;
+               cmInitTimers(&(schCb[inst].statistics.dlTotalPrbUsage->periodTimer), 1);
+
+               /* Start timer */
+               schStartTmr(&schCb[inst], (PTR)(schCb[inst].statistics.dlTotalPrbUsage), \
+                     EVENT_DL_TOTAL_PRB_USAGE_TMR, schCb[inst].statistics.dlTotalPrbUsage->periodicity);
+
+               isDlTotlPrbUseCfgd = true;
+               break;
+            }
+
+         case SCH_UL_TOTAL_PRB_USAGE:
+            {
+               /* Check if duplicate configuration */
+               if(schCb[inst].statistics.ulTotalPrbUsage)
+               {
+                  DU_LOG("\nERROR   -->  SCH : SCH_UL_TOTAL_PRB_USAGE stats already configured");
+                  rsp = RSP_NOK;
+                  cause = DUPLICATE_ENTRY;
+               }
+
+               /* Allocate memory */
+               SCH_ALLOC(schCb[inst].statistics.ulTotalPrbUsage, sizeof(TotalPrbUsage));
+               if(!schCb[inst].statistics.ulTotalPrbUsage)
+               {
+                  DU_LOG("\nERROR   -->  SCH : Memory allocation failed for ulTotalPrbUsage in \
+                        SchProcStatsReq()");
+                  rsp = RSP_NOK;
+                  cause = RESOURCE_UNAVAILABLE;
+                  break;
+               }
+
+               /* Initialize */
+               memset(schCb[inst].statistics.ulTotalPrbUsage, 0, sizeof(TotalPrbUsage));
+
+               /* Configure */
+               schCb[inst].statistics.ulTotalPrbUsage->schInst = inst;
+               schCb[inst].statistics.ulTotalPrbUsage->periodicity = statsReq->statsList[idx].periodicity;
+               cmInitTimers(&(schCb[inst].statistics.ulTotalPrbUsage->periodTimer), 1);
+
+               /* Start timer */
+               schStartTmr(&schCb[inst], (PTR)(schCb[inst].statistics.ulTotalPrbUsage), \
+                     EVENT_UL_TOTAL_PRB_USAGE_TMR, schCb[inst].statistics.ulTotalPrbUsage->periodicity);
+
+               isUlTotlPrbUseCfgd = true;
+               break;
+            }
+         default:
+            {
+               DU_LOG("\nERROR   -->  SCH : Invalid statistics type [%d]", statsReq->statsList[idx].type);
+               rsp = RSP_NOK;
+               cause = PARAM_INVALID;
+            }
+      } /* End of switch */
+
+      if(rsp == RSP_NOK)
+      {
+         /* If failed to configure any KPI, then clear configuration of other
+          * KPIs that were configured successfully as part of this statsReq */
+         if(isDlTotlPrbUseCfgd)
+         {
+            if((schChkTmr((PTR)(schCb[inst].statistics.dlTotalPrbUsage), EVENT_DL_TOTAL_PRB_USAGE_TMR)) == FALSE)
+            {
+               schStopTmr(&schCb[inst], (PTR)(schCb[inst].statistics.dlTotalPrbUsage), EVENT_DL_TOTAL_PRB_USAGE_TMR);
+            }
+            SCH_FREE(schCb[inst].statistics.dlTotalPrbUsage, sizeof(TotalPrbUsage));
+         }
+
+         if(isUlTotlPrbUseCfgd)
+         {
+            if((schChkTmr((PTR)(schCb[inst].statistics.ulTotalPrbUsage), EVENT_UL_TOTAL_PRB_USAGE_TMR)) == FALSE)
+            {
+               schStopTmr(&schCb[inst], (PTR)(schCb[inst].statistics.ulTotalPrbUsage), EVENT_UL_TOTAL_PRB_USAGE_TMR);
+            }
+            SCH_FREE(schCb[inst].statistics.ulTotalPrbUsage, sizeof(TotalPrbUsage));
+         }
+         break;
+      }
+   } /* End of FOR */
+
+   SCH_FREE(statsReq, sizeof(SchStatsReq));
+
+   SchSendStatsRspToMac(inst, rsp, cause);
+
+   return ROK;
+} /* End of SchProcStatsReq */
+
 /**********************************************************************
   End of file
  **********************************************************************/