Fix for segmentation fault in CLA [Issue-ID: ODUHIGH-348]
[o-du/l2.git] / src / 5gnrsch / sch_slot_ind.c
index 0682695..6d8b7da 100644 (file)
@@ -49,9 +49,7 @@ SchMacDlAllocFunc schMacDlAllocOpts[] =
    packSchMacDlAlloc
 };
 
-extern SchCb schCb[SCH_MAX_INST];
-extern int schFillRar(RarAlloc *rarAlloc, uint16_t raRnti, uint16_t pci, uint8_t
-      offsetPointA);
+SchCb schCb[SCH_MAX_INST];
 
 /*******************************************************************
  *
@@ -113,11 +111,98 @@ void schCalcSlotValues(SlotIndInfo slotInd, SchSlotValue *schSlotValue)
     *        on PHY_DELTA + SCHED_DELTA + BO_DELTA                 *
     ****************************************************************/
 
-   ADD_DELTA_TO_TIME(slotInd,schSlotValue->currentTime,PHY_DELTA);
-   ADD_DELTA_TO_TIME(slotInd,schSlotValue->broadcastTime,PHY_DELTA+SCHED_DELTA);
-   ADD_DELTA_TO_TIME(slotInd,schSlotValue->rarTime,PHY_DELTA+SCHED_DELTA);
-   ADD_DELTA_TO_TIME(slotInd,schSlotValue->msg4Time,PHY_DELTA+SCHED_DELTA);
-   ADD_DELTA_TO_TIME(slotInd,schSlotValue->dlMsgTime,PHY_DELTA+SCHED_DELTA+BO_DELTA);
+   ADD_DELTA_TO_TIME(slotInd, schSlotValue->currentTime, PHY_DELTA_DL);
+   ADD_DELTA_TO_TIME(slotInd, schSlotValue->broadcastTime, PHY_DELTA_DL + SCHED_DELTA);
+   ADD_DELTA_TO_TIME(slotInd, schSlotValue->rarTime, PHY_DELTA_DL + SCHED_DELTA);
+   ADD_DELTA_TO_TIME(slotInd, schSlotValue->dlMsgTime, PHY_DELTA_DL + SCHED_DELTA);
+}
+
+/*******************************************************************
+ *
+ * @brief 
+ *
+ * @details
+ *
+ *    Function : 
+ *
+ *    Functionality:
+ *
+ * @params[in] 
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+uint8_t schFillBoGrantDlSchedInfo(SchCellCb *cell, DlSchedInfo *dlSchedInfo, DlMsgAlloc *dlMsgAlloc)
+{
+   uint8_t ueIdx, lcIdx;
+   uint16_t slot;
+   uint16_t crnti = 0;
+   uint32_t accumalatedSize = 0;
+   SchUeCb *ueCb = NULLP;
+
+   while(cell->boIndBitMap)
+   {
+      slot = dlSchedInfo->schSlotValue.dlMsgTime.slot;
+
+      GET_RIGHT_MOST_SET_BIT(cell->boIndBitMap, ueIdx);
+      GET_CRNTI(crnti,ueIdx);
+      ueCb = &cell->ueCb[ueIdx-1];
+
+      /* allocate PDCCH and PDSCH resources for the ue */
+      SCH_ALLOC(dlMsgAlloc, sizeof(DlMsgAlloc));
+      if(!dlMsgAlloc)
+      {
+         DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for ded DL msg alloc");
+         return RFAILED;
+      }
+      memset(dlMsgAlloc, 0, sizeof(DlMsgAlloc));
+      dlSchedInfo->dlMsgAlloc = dlMsgAlloc;
+      dlMsgAlloc->crnti = crnti;
+
+      /* Dl ded Msg info is copied, this was earlier filled in macSchDlRlcBoInfo */
+      memcpy(&dlMsgAlloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
+           sizeof(DlMsgInfo));
+
+      /* scheduled LC data fill */
+      dlMsgAlloc->numLc = 0;
+      for(lcIdx = 0; lcIdx < MAX_NUM_LC; lcIdx++)
+      {
+         if(ueCb->dlInfo.dlLcCtxt[lcIdx].bo)
+         {
+            dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].lcId = lcIdx;
+
+            /* calculation for BO includse RLC and MAC header size */
+            dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes = \
+                                                                ueCb->dlInfo.dlLcCtxt[lcIdx].bo + MAC_HDR_SIZE;
+            accumalatedSize += dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc].schBytes;
+            dlMsgAlloc->numLc++;
+         }
+         ueCb->dlInfo.dlLcCtxt[lcIdx].bo = 0;
+      }
+
+      /* pdcch and pdsch data is filled */
+      schDlRsrcAllocDlMsg(dlMsgAlloc, cell, crnti, &accumalatedSize, slot);
+      /* Calculated TB size could be less than the total size requested.
+       * Hence, updated the scheduled bytes report. Following is valid only for
+       * one LC.
+       * TODO : Update the scheduling byte report for multiple LC based on QCI
+       * and Priority */
+      dlMsgAlloc->lcSchInfo[dlMsgAlloc->numLc -1].schBytes = accumalatedSize;
+
+      /* PUCCH resource */
+      schAllocPucchResource(cell, dlMsgAlloc->crnti, slot);
+
+      /* Free the dl ded msg info allocated in macSchDlRlcBoInfo */
+      SCH_FREE(cell->schDlSlotInfo[dlSchedInfo->schSlotValue.dlMsgTime.slot]->dlMsgInfo, \
+           sizeof(DlMsgInfo));
+      cell->schDlSlotInfo[dlSchedInfo->schSlotValue.dlMsgTime.slot]->dlMsgInfo = NULL;
+
+      /* after allocation is done, unset the bo bit for that ue */
+      UNSET_ONE_BIT(ueIdx, cell->boIndBitMap);
+   }
+
+   return ROK;
 }
 
 /*******************************************************************
@@ -138,40 +223,50 @@ void schCalcSlotValues(SlotIndInfo slotInd, SchSlotValue *schSlotValue)
  * ****************************************************************/
 uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
 {
-   int ret = ROK;
-   uint8_t ssb_rep, ueIdx, lcgIdx;
-   uint16_t slot, sfnSlot = 0;
+   uint8_t  ssb_rep, ueIdx, lcgIdx, ret = ROK;
+   uint16_t slot;
    DlSchedInfo dlSchedInfo;
-   memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
-   DlBrdcstAlloc *dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
-   RarAlloc  *rarAlloc = NULLP;
-   Msg4Alloc *msg4Alloc = NULLP;
-   dlBrdcstAlloc->ssbTrans = NO_SSB;
-   dlBrdcstAlloc->sib1Trans = NO_SIB1;
+   DlBrdcstAlloc *dlBrdcstAlloc = NULLP;
+   RarAlloc   *rarAlloc = NULLP;
+   DlMsgAlloc  *msg4Alloc = NULLP;
+   DlMsgAlloc *dlMsgAlloc = NULLP;
+   SchCellCb  *cell = NULLP;
 
-   SchCellCb *cell = schCb[schInst].cells[schInst];
 
+   memset(&dlSchedInfo,0,sizeof(DlSchedInfo));
+   dlSchedInfo.dlMsgAlloc = NULLP;
    schCalcSlotValues(*slotInd, &dlSchedInfo.schSlotValue);
+   dlBrdcstAlloc = &dlSchedInfo.brdcstAlloc;
+   dlBrdcstAlloc->ssbTrans = NO_SSB;
+   dlBrdcstAlloc->sib1Trans = NO_SIB1;
 
+   cell = schCb[schInst].cells[schInst];
+   if(cell == NULLP)
+   {
+      DU_LOG("\nERROR  -->  SCH : Cell Does not exist");
+      return RFAILED;
+   }
    ssb_rep = cell->cellCfg.ssbSchCfg.ssbPeriod;
    memcpy(&cell->slotInfo, slotInd, sizeof(SlotIndInfo));
    dlBrdcstAlloc->ssbIdxSupported = 1;
 
-   sfnSlot = ((dlSchedInfo.schSlotValue.broadcastTime.sfn * 10) +
-        dlSchedInfo.schSlotValue.broadcastTime.slot);
-
    slot = dlSchedInfo.schSlotValue.currentTime.slot;
 
    dlSchedInfo.cellId = cell->cellId;
 
    /* Identify SSB ocassion*/
-   if (sfnSlot % SCH_MIB_TRANS == 0)
+   if ((dlSchedInfo.schSlotValue.broadcastTime.sfn % SCH_MIB_TRANS == 0) && (dlSchedInfo.schSlotValue.broadcastTime.slot ==0))
    {
       dlBrdcstAlloc->ssbTrans = SSB_TRANSMISSION;
+      if(!cell->firstSsbTransmitted)
+         cell->firstSsbTransmitted = true;
    }
-   else if (sfnSlot % ssb_rep == 0)
+   else if (cell->firstSsbTransmitted) 
    {
-      dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
+      if((ssb_rep == 5) && ((dlSchedInfo.schSlotValue.broadcastTime.slot == 0 || dlSchedInfo.schSlotValue.broadcastTime.slot == 10)))
+         dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
+      else if((dlSchedInfo.schSlotValue.broadcastTime.sfn % (ssb_rep/10) == 0) && dlSchedInfo.schSlotValue.broadcastTime.slot == 0)
+         dlBrdcstAlloc->ssbTrans = SSB_REPEAT;
    }
    else
    {
@@ -179,19 +274,26 @@ uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
    }
 
    /* Identify SIB1 occasions */
-   if(sfnSlot % cell->cellCfg.sib1SchCfg.sib1NewTxPeriod == 0)
+   if((dlSchedInfo.schSlotValue.broadcastTime.sfn % SCH_SIB1_TRANS == 0) && (dlSchedInfo.schSlotValue.broadcastTime.slot ==0))
    {
       dlBrdcstAlloc->sib1Trans = SIB1_TRANSMISSION;
+      if(!cell->firstSib1Transmitted)
+         cell->firstSib1Transmitted = true;
    }
-   else if (sfnSlot % cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod == 0)
+   else if (cell->firstSib1Transmitted) 
    {
-      dlBrdcstAlloc->sib1Trans = SIB1_REPITITION;
+      if((dlSchedInfo.schSlotValue.broadcastTime.sfn % (cell->cellCfg.sib1SchCfg.sib1RepetitionPeriod/10) == 0) &&
+            (dlSchedInfo.schSlotValue.broadcastTime.slot == 0))
+      {
+         dlBrdcstAlloc->sib1Trans = SIB1_REPITITION;
+      }
    }
    else
    {
       /* not SIB1 occassion */
    }
 
+
    if(dlBrdcstAlloc->ssbTrans || dlBrdcstAlloc->sib1Trans)
    {
       dlSchedInfo.isBroadcastPres = true;
@@ -199,8 +301,8 @@ uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
       ret = schBroadcastAlloc(cell,dlBrdcstAlloc,slot);
       if(ret != ROK)
       {
-        DU_LOG("\nschBroadcastAlloc failed");
-        return (ret);
+         DU_LOG("\nERROR  -->  SCH : schBroadcastAlloc failed");
+         return ret;
       }
    }
 
@@ -211,8 +313,8 @@ uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
       SCH_ALLOC(rarAlloc, sizeof(RarAlloc));
       if(!rarAlloc)
       {
-        DU_LOG("\nMAC: Memory Allocation failed for RAR alloc");
-        return RFAILED;
+         DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for RAR alloc");
+         return RFAILED;
       }
 
       dlSchedInfo.rarAlloc = rarAlloc;
@@ -224,37 +326,41 @@ uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
       schFillRar(rarAlloc,
            cell->schDlSlotInfo[slot]->rarInfo->raRnti,
            cell->cellCfg.phyCellId,
-           cell->cellCfg.ssbSchCfg.ssbOffsetPointA);
+           cell->cellCfg.ssbSchCfg.ssbOffsetPointA,
+       dlBrdcstAlloc->ssbTrans,
+       dlBrdcstAlloc->sib1Trans);
 
-      SCH_FREE(cell->schDlSlotInfo[slot]->rarInfo,sizeof(RarAlloc));
+      SCH_FREE(cell->schDlSlotInfo[slot]->rarInfo,sizeof(RarInfo));
       cell->schDlSlotInfo[slot]->rarInfo = NULLP;
    }
 
    /* check for MSG4 */
-   if(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info != NULLP)
+   if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
+      (cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
    {
-      slot = dlSchedInfo.schSlotValue.msg4Time.slot;
-      SCH_ALLOC(msg4Alloc, sizeof(Msg4Alloc));
+      slot = dlSchedInfo.schSlotValue.dlMsgTime.slot;
+
+      SCH_ALLOC(msg4Alloc, sizeof(DlMsgAlloc));
       if(!msg4Alloc)
       {
-        DU_LOG("\nMAC: Memory Allocation failed for msg4 alloc");
-        return RFAILED;
+         DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4 alloc");
+         return RFAILED;
       }
 
-      dlSchedInfo.msg4Alloc = msg4Alloc;
+      dlSchedInfo.dlMsgAlloc = msg4Alloc;
 
       /* Msg4 info is copied, this was earlier filled in macSchDlRlcBoInfo */
-      memcpy(&msg4Alloc->msg4Info, cell->schDlSlotInfo[slot]->msg4Info, \
-           sizeof(Msg4Info));
+      memcpy(&msg4Alloc->dlMsgInfo, cell->schDlSlotInfo[slot]->dlMsgInfo, \
+           sizeof(DlMsgInfo));
 
       /* pdcch and pdsch data is filled */
-      schDlRsrcAllocMsg4(msg4Alloc, cell, dlSchedInfo.schSlotValue.msg4Time.slot); 
+      schDlRsrcAllocMsg4(msg4Alloc, cell, dlSchedInfo.schSlotValue.dlMsgTime.slot, dlBrdcstAlloc->ssbTrans, dlBrdcstAlloc->sib1Trans); 
 
       /* PUCCH resource */
-      schAllocPucchResource(cell, msg4Alloc->msg4Info.crnti, dlSchedInfo.schSlotValue.msg4Time.slot);
+      schAllocPucchResource(cell, msg4Alloc->dlMsgInfo.crnti, dlSchedInfo.schSlotValue.dlMsgTime.slot);
 
-      SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info, sizeof(Msg4Info));
-      cell->schDlSlotInfo[dlSchedInfo.schSlotValue.msg4Time.slot]->msg4Info = NULL;
+      SCH_FREE(cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo, sizeof(DlMsgInfo));
+      cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo = NULL;
    }
    /* check if UL grant must be sent in this slot for a SR/BSR that had been received */
    for(ueIdx=0; ueIdx<cell->numActvUe; ueIdx++)
@@ -274,37 +380,44 @@ uint8_t schProcessSlotInd(SlotIndInfo *slotInd, Inst schInst)
       for(lcgIdx=0; lcgIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
       {
         totDataReq+= ueCb->bsrInfo[lcgIdx].dataVol;
-       ueCb->bsrInfo[lcgIdx].dataVol = 0;
+        ueCb->bsrInfo[lcgIdx].dataVol = 0;
       }
       if(totDataReq > 0) /* UL grant must be provided for this UE in this slot */
       {
          SchPuschInfo schPuschInfo;
          memset(&schPuschInfo, 0, sizeof(SchPuschInfo));
 
-        SCH_ALLOC(dciInfo, sizeof(DciInfo));
-        if(!dciInfo)
-        {
-           DU_LOG("\nMAC: Memory Allocation failed for dciInfo alloc");
-           return RFAILED;
-        }
-        memset(dciInfo,0,sizeof(DciInfo));
-        /* update the SFN and SLOT */
-        memcpy(&dlSchedInfo.schSlotValue.ulDciTime, slotInd, sizeof(SlotIndInfo));
-        slot = dlSchedInfo.schSlotValue.ulDciTime.slot;
-        /* Update PUSCH allocation */
-        schFillPuschAlloc(ueCb, slot, totDataReq, &schPuschInfo);
-        /* Fill DCI for UL grant */
-        schFillUlDci(ueCb, schPuschInfo, dciInfo);
-        memcpy(&dciInfo->slotIndInfo, &dlSchedInfo.schSlotValue.ulDciTime, sizeof(SlotIndInfo));
+         SCH_ALLOC(dciInfo, sizeof(DciInfo));
+         if(!dciInfo)
+         {
+            DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
+            return RFAILED;
+         }
+         memset(dciInfo,0,sizeof(DciInfo));
+         /* update the SFN and SLOT */
+         memcpy(&dlSchedInfo.schSlotValue.ulDciTime, slotInd, sizeof(SlotIndInfo));
+         slot = dlSchedInfo.schSlotValue.ulDciTime.slot;
+         /* Update PUSCH allocation */
+         schFillPuschAlloc(ueCb, slot, totDataReq, &schPuschInfo);
+         /* Fill DCI for UL grant */
+         schFillUlDci(ueCb, schPuschInfo, dciInfo);
+         memcpy(&dciInfo->slotIndInfo, &dlSchedInfo.schSlotValue.ulDciTime, sizeof(SlotIndInfo));
          dlSchedInfo.ulGrant = dciInfo;
       }
    }
 
+   /* Check for pending BO grant for LC */
+   if((cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo != NULLP) &&
+      (!cell->schDlSlotInfo[dlSchedInfo.schSlotValue.dlMsgTime.slot]->dlMsgInfo->isMsg4Pdu))
+   {
+      schFillBoGrantDlSchedInfo(cell, &dlSchedInfo, dlMsgAlloc);
+   }
+
    /* send msg to MAC */
    ret = sendDlAllocToMac(&dlSchedInfo, schInst);
    if(ret != ROK)
    {
-      DU_LOG("\nSending DL Broadcast allocation from SCH to MAC failed");
+      DU_LOG("\nERROR  -->  SCH : Sending DL Broadcast allocation from SCH to MAC failed");
       return (ret);
    }