DL Resource allocation for msg4
[o-du/l2.git] / src / 5gnrsch / sch_common.c
index 8907fe9..6c00b57 100644 (file)
@@ -60,8 +60,18 @@ File:     sch_common.c
 #include "du_app_mac_inf.h"
 #include "mac_sch_interface.h"
 #include "sch.h"
+#include "sch_utils.h"
 
 extern SchCb schCb[SCH_MAX_INST];
+extern uint16_t prachCfgIdxTable[MAX_PRACH_CONFIG_IDX][8];
+extern uint16_t numRbForPrachTable[MAX_RACH_NUM_RB_IDX][5];
+
+SchMacUlSchInfoFunc schMacUlSchInfoOpts[] =
+{
+       packSchMacUlSchInfo,
+       MacProcUlSchInfo,
+       packSchMacUlSchInfo
+};
 
 /**
  * @brief Time domain allocation for SSB
@@ -72,8 +82,7 @@ extern SchCb schCb[SCH_MAX_INST];
  *     
  *     This function handles common scheduling for DL
  *     
- *  @param[in]  schCellCb *cell, cell cb
- *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
+ *  @param[in]  uint8_t scs, uint8_t *ssbStartSym
  *  @return  void
  **/
 void ssbDlTdAlloc(uint8_t scs, uint8_t *ssbStartSymb)
@@ -98,12 +107,13 @@ void ssbDlTdAlloc(uint8_t scs, uint8_t *ssbStartSymb)
                        DU_LOG("\nSCS %d is currently not supported", scs);
        }
 }
+
 /**
  * @brief common resource allocation for SSB
  *
  * @details
  *
- *     Function : schCmnDlAlloc
+ *     Function : schBroadcastAlloc
  *     
  *     This function handles common scheduling for DL
  *     
@@ -111,7 +121,8 @@ void ssbDlTdAlloc(uint8_t scs, uint8_t *ssbStartSymb)
  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
  *  @return  void
  **/
-uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
+uint8_t schBroadcastAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc,
+        uint16_t slot)
 {
        /* schedule SSB */
        uint8_t scs, ssbStartPrb, ssbStartSymb, idx;
@@ -119,12 +130,11 @@ uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
        SchDlAlloc *dlAlloc;
        SsbInfo ssbInfo;
 
-       dlAlloc = cell->dlAlloc[cell->slotInfo.slot];
+       dlAlloc = cell->dlAlloc[slot];
        if(dlBrdcstAlloc->ssbTrans)
        {
                scs = cell->cellCfg.ssbSchCfg.scsCommon;
-               ssbStartPrb = \
-                  ((cell->cellCfg.ssbSchCfg.ssbSubcOffset)-(cell->cellCfg.ssbSchCfg.ssbOffsetPointA))/SCH_NUM_SC_PRB;
+               ssbStartPrb = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
 
                memset(ssbStartSymbArr, 0, SCH_MAX_SSB_BEAM);
                ssbDlTdAlloc(scs, ssbStartSymbArr);
@@ -133,10 +143,10 @@ uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
                for(idx=0; idx<dlBrdcstAlloc->ssbIdxSupported; idx++)
                {
                        ssbInfo.ssbIdx = idx;
-                       ssbInfo.fdAlloc.ssbStartPrbIdx = ssbStartPrb;
-                       ssbInfo.fdAlloc.ssbPrbDuration = SCH_SSB_PRB_DURATION;
-                       ssbInfo.tdAlloc.ssbStartSymbIdx = ssbStartSymb;
-                       ssbInfo.tdAlloc.ssbSymbolDuration = SCH_SSB_SYMB_DURATION;
+                       ssbInfo.fdAlloc.startPrb  = ssbStartPrb;
+                       ssbInfo.fdAlloc.numPrb    = SCH_SSB_PRB_DURATION;
+                       ssbInfo.tdAlloc.startSymb = ssbStartSymb;
+                       ssbInfo.tdAlloc.numSymb   = SCH_SSB_SYMB_DURATION;
                        dlBrdcstAlloc->ssbInfo[idx] = ssbInfo;
                        dlAlloc->ssbInfo[idx] = ssbInfo;
 
@@ -146,7 +156,7 @@ uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
                dlAlloc->ssbIdxSupported = dlBrdcstAlloc->ssbIdxSupported;
                for(idx=ssbStartSymb; idx<ssbStartSymb+SCH_SSB_SYMB_DURATION; idx++)
                {
-                       dlAlloc->assignedPrb[idx] = SCH_SSB_PRB_DURATION; 
+                       dlAlloc->assignedPrb[idx] = ssbStartPrb + SCH_SSB_PRB_DURATION + 1; /* +1 for kSsb */
                }
 
        }
@@ -154,12 +164,305 @@ uint8_t schCmnDlAlloc(SchCellCb *cell, DlBrdcstAlloc *dlBrdcstAlloc)
        /* SIB1 allocation */
        if(dlBrdcstAlloc->sib1Trans)
        {
-          memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->cellCfg.sib1SchCfg.sib1PdcchCfg, sizeof(Sib1PdcchCfg)); 
-          memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdschCfg, &cell->cellCfg.sib1SchCfg.sib1PdschCfg, sizeof(Sib1PdschCfg)); 
+               dlAlloc->sib1Pres = true;
+               for(idx=0; idx<SCH_SYMBOL_PER_SLOT; idx++)
+               {
+                       dlAlloc->assignedPrb[idx] = ssbStartPrb + SCH_SSB_PRB_DURATION + 1 + 10; /* 10 PRBs for sib1 */
+               }
+          memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->cellCfg.sib1SchCfg.sib1PdcchCfg, sizeof(PdcchCfg)); 
+          memcpy(&dlBrdcstAlloc->sib1Alloc.sib1PdschCfg, &cell->cellCfg.sib1SchCfg.sib1PdschCfg, sizeof(PdschCfg)); 
        }
        return ROK;
 }
 
+/*******************************************************************
+ *
+ * @brief Handles sending UL scheduler info to MAC 
+ *
+ * @details
+ *
+ *    Function : sendUlSchInfoToMac
+ *
+ *    Functionality:
+ *     Sends UL Sch info to MAC from SCH
+ *
+ * @params[in] 
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+int sendUlSchInfoToMac(UlSchInfo *ulSchInfo, Inst inst)
+{
+       Pst pst;
+
+   memset(&pst, 0, sizeof(Pst));
+   SCH_FILL_RSP_PST(pst, inst);
+       pst.event = EVENT_UL_SCH_INFO;
+
+       return(*schMacUlSchInfoOpts[pst.selector])(&pst, ulSchInfo);
+
+}
+/**
+ * @brief resource allocation for PRACH
+ *
+ * @details
+ *
+ *     Function : schPrachResAlloc
+ *     
+ *     This function handles PRACH allocation
+ *     
+ *  @param[in]  SchCellCb *cell, cell cb
+ *  @param[in]  UlSchInfo *ulSchInfo, UL scheduling info
+ *  @return  void
+ **/
+int schPrachResAlloc(SchCellCb *cell, UlSchInfo *ulSchInfo)
+{
+   uint8_t  numPrachRb = 0;
+       uint8_t  numRa = 0;
+       uint8_t  freqStart = 0;
+       uint16_t sfn = 0;
+       uint16_t slot = 0;
+       uint8_t  prachCfgIdx = 0;
+       uint8_t  prachFormat = 0;
+       uint8_t  x = 0;
+       uint8_t  y = 0;
+       uint16_t prachSubframe = 0;
+       uint8_t  prachStartSymbol = 0;
+       uint8_t  prachOcas = 0;
+       uint8_t  dataType = 0;
+       uint8_t  idx = 0;
+       SchUlAlloc *ulAlloc = NULLP;
+
+       sfn  = cell->slotInfo.sfn;
+       slot = cell->slotInfo.slot; 
+       ulAlloc = cell->ulAlloc[cell->slotInfo.slot];
+       prachCfgIdx = cell->cellCfg.schRachCfg.prachCfgIdx;
+
+    /* derive the prachCfgIdx table paramters */
+       x                = prachCfgIdxTable[prachCfgIdx][1];
+       y                = prachCfgIdxTable[prachCfgIdx][2];
+       prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
+       
+       if((sfn%x) != y)
+       {
+          /* prach occasion does not lie in this SFN */
+               DU_LOG("\nPRACH ocassion doesn't lie in this SFN");
+          return RFAILED;
+       }
+       /* check for subFrame number */
+       if ((1 << slot) & prachSubframe)
+       {
+      /* prach ocassion present in this subframe */
+
+               prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
+               prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
+               prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
+
+               /* freq domain resource determination for RACH*/
+               freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
+               /* numRa determined as 𝑛 belonging {0,1,.., M − 1}, 
+                * where M is given by msg1Fdm */
+               numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
+               for(idx=0; idx<MAX_RACH_NUM_RB_IDX; idx++)
+               {
+         if(numRbForPrachTable[idx][0] == cell->cellCfg.schRachCfg.rootSeqIdx)
+                          break;
+               }
+               numPrachRb = numRbForPrachTable[idx][3];
+               dataType |= SCH_DATATYPE_PRACH;
+               /* Considering first slot in the frame for PRACH */
+               idx = 0;
+               ulAlloc->assignedPrb[idx] = freqStart+numPrachRb;
+       }
+
+       /* Fill UL SCH Info */
+       ulSchInfo->cellId = cell->cellId;
+       ulSchInfo->slotIndInfo.sfn = sfn;
+   ulSchInfo->slotIndInfo.slot = slot;
+       ulSchInfo->dataType = dataType;
+       /* prach info */
+       ulSchInfo->prachSchInfo.numPrachOcas   = prachOcas;
+       ulSchInfo->prachSchInfo.prachFormat    = prachFormat;
+       ulSchInfo->prachSchInfo.numRa          = numRa;
+       ulSchInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
+
+       return ROK;
+}
+
+/**
+ * @brief resource allocation for UL
+ *
+ * @details
+ *
+ *     Function : schUlResAlloc
+ *     
+ *     This function handles UL Resource allocation
+ *     
+ *  @param[in]  SchCellCb *cell, cellCb
+ *  @return  void
+ **/
+uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst)
+{
+   int ret = ROK;
+   UlSchInfo ulSchInfo;
+       SchUlAlloc *ulAlloc;
+
+   /* Schedule resources for PRACH */
+       schPrachResAlloc(cell, &ulSchInfo);
+
+       ulAlloc = cell->ulAlloc[cell->slotInfo.slot]; 
+       
+       if(ulAlloc->schPuschInfo)
+       {
+               ulSchInfo.dataType |= SCH_DATATYPE_PUSCH;
+               memcpy(&ulSchInfo.schPuschInfo, ulAlloc->schPuschInfo,
+                               sizeof(SchPuschInfo));
+               SCH_FREE(ulAlloc->schPuschInfo, sizeof(SchPuschInfo));
+       }
+
+       //send msg to MAC
+   ret = sendUlSchInfoToMac(&ulSchInfo, schInst);
+   if(ret != ROK)
+   {
+      DU_LOG("\nSending UL Sch info from SCH to MAC failed");
+   }
+    
+       return ret;
+}
+
+/*******************************************************************
+ *
+ * @brief Fills pdcch and pdsch info for msg4
+ *
+ * @details
+ *
+ *    Function : schDlRsrcAllocMsg4
+ *
+ *    Functionality:
+ *       Fills pdcch and pdsch info for msg4
+ *
+ * @params[in] 
+ * @return ROK     - success
+ *         RFAILED - failure
+ *
+ * ****************************************************************/
+uint8_t schDlRsrcAllocMsg4(Msg4Alloc *msg4Alloc, SchCellCb *cell, uint16_t slot)
+{
+   uint8_t coreset0Idx = 0;
+   uint8_t numRbs = 0;
+   uint8_t firstSymbol = 0;
+   uint8_t numSymbols = 0;
+   uint8_t offset = 0;
+   uint8_t offsetPointA;
+   uint8_t FreqDomainResource[6] = {0};
+   SchBwpDlCfg *initialBwp;
+
+   PdcchCfg *pdcch = &msg4Alloc->msg4PdcchCfg;
+   PdschCfg *pdsch = &msg4Alloc->msg4PdschCfg;
+
+   initialBwp   = &cell->cellCfg.schInitialDlBwp;
+   offsetPointA = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
+   coreset0Idx  = initialBwp->pdcchCommon.raSearchSpace.coresetId;
+
+   /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
+   numRbs        = coresetIdxTable[coreset0Idx][1];
+   numSymbols    = coresetIdxTable[coreset0Idx][2];
+   offset        = coresetIdxTable[coreset0Idx][3];
+
+   /* calculate time domain parameters */
+   uint16_t mask = 0x2000;
+   for(firstSymbol=0; firstSymbol<14;firstSymbol++)
+   {
+      if(initialBwp->pdcchCommon.raSearchSpace.monitoringSymbol & mask)
+         break;
+      else
+         mask = mask>>1;
+   }
+
+   /* calculate the PRBs */
+   calculatePRB( ((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
+
+   /* fill the PDCCH PDU */
+   pdcch->pdcchBwpCfg.BWPSize = initialBwp->bwp.numPrb;
+   pdcch->pdcchBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
+   pdcch->pdcchBwpCfg.subcarrierSpacing = initialBwp->bwp.scs;
+   pdcch->pdcchBwpCfg.cyclicPrefix = initialBwp->bwp.cyclicPrefix;
+   pdcch->coreset0Cfg.startSymbolIndex = firstSymbol;
+   pdcch->coreset0Cfg.durationSymbols = numSymbols;
+   memcpy(pdcch->coreset0Cfg.freqDomainResource,FreqDomainResource,6);
+   pdcch->coreset0Cfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
+   pdcch->coreset0Cfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
+   pdcch->coreset0Cfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
+   pdcch->coreset0Cfg.coreSetType = 0;
+   pdcch->coreset0Cfg.coreSet0Size = numRbs;
+   pdcch->coreset0Cfg.shiftIndex = cell->cellCfg.phyCellId;
+   pdcch->coreset0Cfg.precoderGranularity = 0; /* sameAsRegBundle */
+   pdcch->numDlDci = 1;
+   pdcch->dci.rnti = cell->dlAlloc[slot]->msg4Info->crnti;
+   pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
+   pdcch->dci.scramblingRnti = 0;
+   pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
+   pdcch->dci.aggregLevel = 4;
+   pdcch->dci.beamPdcchInfo.numPrgs = 1;
+   pdcch->dci.beamPdcchInfo.prgSize = 1;
+   pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
+   pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
+   pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
+   pdcch->dci.txPdcchPower.powerValue = 0;
+   pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
+
+   /* fill the PDSCH PDU */
+   uint8_t cwCount = 0;
+   pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
+   pdsch->rnti = cell->dlAlloc[slot]->msg4Info->crnti;
+   pdsch->pduIndex = 0;
+   pdsch->pdschBwpCfg.BWPSize = initialBwp->bwp.numPrb;
+   pdsch->pdschBwpCfg.BWPStart = initialBwp->bwp.firstPrb;
+   pdsch->numCodewords = 1;
+   for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
+   {
+      pdsch->codeword[cwCount].targetCodeRate = 308;
+      pdsch->codeword[cwCount].qamModOrder = 2;
+      pdsch->codeword[cwCount].mcsIndex = 4; /* mcs configured to 4 */
+      pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
+      pdsch->codeword[cwCount].rvIndex = 0;
+      /* 38.214: Table 5.1.3.2-1,  divided by 8 to get the value in bytes */
+      /* TODO : Calculate tbSize based of DL CCCH msg size */
+      pdsch->codeword[cwCount].tbSize = 2664/8;
+   }
+   pdsch->dataScramblingId = cell->cellCfg.phyCellId;
+   pdsch->numLayers = 1;
+   pdsch->transmissionScheme = 0;
+   pdsch->refPoint = 0;
+   pdsch->dmrs.dlDmrsSymbPos = 2;
+   pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
+   pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
+   pdsch->dmrs.scid = 0;
+   pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
+   pdsch->dmrs.dmrsPorts = 0;
+   pdsch->freqAlloc.resourceAlloc = 1; /* RAT type-1 RIV format */
+   /* the RB numbering starts from coreset0, and PDSCH is always above SSB */
+   pdsch->freqAlloc.rbStart = offset + SCH_SSB_PRB_DURATION;
+   /* formula used for calculation of rbSize, 38.213 section 5.1.3.2 *
+    * Ninfo = S . Nre . R . Qm . v                                   *
+    * Nre' = Nsc . NsymPdsch - NdmrsSymb - Noh                       *
+    * Nre = min(156,Nre') . nPrb                                     */
+   /* TODO : Calculate rbSize based on tbSize calculated */
+   pdsch->freqAlloc.rbSize = 34;
+   pdsch->freqAlloc.vrbPrbMapping = 0; /* non-interleaved */
+   pdsch->timeAlloc.startSymbolIndex = 2; /* spec-38.214, Table 5.1.2.1-1 */
+   pdsch->timeAlloc.numSymbols = 12;
+   pdsch->beamPdschInfo.numPrgs = 1;
+   pdsch->beamPdschInfo.prgSize = 1;
+   pdsch->beamPdschInfo.digBfInterfaces = 0;
+   pdsch->beamPdschInfo.prg[0].pmIdx = 0;
+   pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
+   pdsch->txPdschPower.powerControlOffset = 0;
+   pdsch->txPdschPower.powerControlOffsetSS = 0;
+   pdcch->dci.pdschCfg = pdsch;
+   return ROK;
+}
 
 /**********************************************************************
   End of file