+ uint8_t ueId = 0;
+
+ if(raReq->isCFRA)
+ {
+ /* If a UE in handover has triggered CFRA, its UE CB context is already present in SCH,
+ * Hence, no need to create raCb */
+ if(raReq->ueCb && (raReq->ueCb->state == SCH_UE_HANDIN_IN_PROGRESS))
+ {
+ schCb[schInst].cells[schInst]->numActvUe++;
+ SET_ONE_BIT(raReq->ueCb->ueId, schCb[schInst].cells[schInst]->actvUeBitMap);
+ raReq->ueCb->state = SCH_UE_STATE_ACTIVE;
+ }
+ }
+ else
+ {
+ /* Create RA CB only for CB-RA to use for msg3 and msg4 processing */
+ GET_UE_ID(raReq->rachInd->crnti, ueId);
+ schCb[schInst].cells[schInst]->raCb[ueId -1].tcrnti = raReq->rachInd->crnti;
+ schCb[schInst].cells[schInst]->raCb[ueId -1].msg4recvd = FALSE;
+ }
+}
+
+/**
+ * @brief resource allocation for msg3 PUSCH
+ *
+ * @details
+ *
+ * Function : schAllocMsg3Pusch
+ *
+ * This function handles msg3 PUSCH allocation
+ *
+ * @param[in] Inst schInst, SCH instance
+ * @param[in] slot, current slot
+ * @param[out] msg3StartRb
+ * @param[out] msg3NumRb
+ * @return void
+ **/
+SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime)
+{
+ SchCellCb *cell = NULLP;
+ SchUlSlotInfo *schUlSlotInfo = NULLP;
+ uint8_t mcs = DEFAULT_MCS;
+ uint8_t startSymb = 0;
+ uint8_t symbLen = 0;
+ uint16_t startRb = 0;
+ uint16_t numRb = 0;
+ uint16_t tbSize = 0;
+
+ cell = schCb[schInst].cells[schInst];
+ if(cell == NULL)
+ {
+ DU_LOG("\n\nERROR --> SCH : Failed to find cell in schAllocMsg3Pusch");
+ return NULLP;
+ }
+
+ /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
+ startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
+ symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
+
+ startRb = MAX_NUM_RB;
+ tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
+ numRb = schCalcNumPrb(tbSize, mcs, symbLen);
+ numRb++; /* allocating 1 extra RB for now */
+ allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
+
+ /* Fill PUSCH scheduling details in Slot structure */
+ schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
+ SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
+ if(!schUlSlotInfo->schPuschInfo)
+ {
+ DU_LOG("\nERROR --> SCH : Memory allocation failed in schAllocMsg3Pusch");
+ return NULLP;
+ }
+
+ tbSize = 0; /* since nPrb has been incremented, recalculating tbSize */
+ tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
+ tbSize = tbSize / 8 ; /*bits to byte conversion*/
+
+ schUlSlotInfo->schPuschInfo->crnti = crnti;
+ schUlSlotInfo->schPuschInfo->harqProcId = SCH_HARQ_PROC_ID;
+ schUlSlotInfo->schPuschInfo->resAllocType = SCH_ALLOC_TYPE_1;
+ schUlSlotInfo->schPuschInfo->fdAlloc.startPrb = startRb;
+ schUlSlotInfo->schPuschInfo->fdAlloc.numPrb = numRb;
+ schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
+ schUlSlotInfo->schPuschInfo->tdAlloc.numSymb = symbLen;
+ schUlSlotInfo->schPuschInfo->tbInfo.qamOrder = QPSK_MODULATION; /* QPSK modulation */
+ schUlSlotInfo->schPuschInfo->tbInfo.mcs = mcs;
+ schUlSlotInfo->schPuschInfo->tbInfo.mcsTable = SCH_MCS_TABLE_QAM_64;
+ schUlSlotInfo->schPuschInfo->tbInfo.ndi = NEW_TRANSMISSION; /* new transmission */
+ schUlSlotInfo->schPuschInfo->tbInfo.rv = 0;
+ schUlSlotInfo->schPuschInfo->tbInfo.tbSize = tbSize;
+ schUlSlotInfo->schPuschInfo->dmrsMappingType = DMRS_MAP_TYPE_A; /* Setting Type-A */
+ schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
+ schUlSlotInfo->schPuschInfo->dmrsAddPos = DMRS_ADDITIONAL_POS;
+
+ return schUlSlotInfo->schPuschInfo;
+}
+
+/**
+ * @brief Check if a time frame is in RA Response window
+ *
+ * @details
+ *
+ * Function : isInRaRspWindow
+ *
+ * Check if a time frame is in RA Response window
+ *
+ * @param[in] RA request
+ * @param[in] Time frame to check
+ * @param[in] Total number of slot per radio frame
+ * @return true
+ * @return false
+ **/
+RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
+{
+ uint32_t winStartTime, winEndTime, timeToCheck;
+
+ winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
+ winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
+ timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
+
+ /* TODO : check how to handle the wrap around scenario of MAX_SFN */
+ if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
+ return WITHIN_WINDOW;
+ else if(timeToCheck < winStartTime)
+ return WINDOW_YET_TO_START;
+
+ return WINDOW_EXPIRED;
+}
+
+/**
+ * @brief Processes any pending RA request
+ *
+ * @details
+ *
+ * Function : schProcessRaReq
+ *
+ * This function process pending RA request
+ *
+ * @param[in] Current timing of the cell
+ * @return ROK
+ **/
+bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
+{
+ bool k1Found = false, k2Found = false;
+ uint8_t k0TblIdx = 0, k1TblIdx = 0, k2TblIdx = 0;
+ uint8_t k0Index = 0, k1Index = 0, k2Index = 0;
+ uint8_t k0 = 0, k1 = 0, k2 = 0;
+ uint8_t numK1 = 0;
+ uint8_t puschMu = 0;
+ uint8_t msg3Delta = 0, msg3MinSchTime = 0;
+#ifdef NR_TDD
+ uint8_t totalCfgSlot = 0;
+#endif
+ uint16_t dciSlot = 0, rarSlot = 0;
+ SlotTimingInfo dciTime, rarTime, msg3Time, pucchTime;
+ RarAlloc *dciSlotAlloc = NULLP; /* Stores info for transmission of PDCCH for RAR */
+ RarAlloc *rarSlotAlloc = NULLP; /* Stores info for transmission of RAR PDSCH */
+ SchPuschInfo *msg3PuschInfo = NULLP; /* Stores MSG3 PUSCH scheduling information */
+ SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;
+ SchK2TimingInfoTbl *msg3K2InfoTbl=NULLP;
+ RaRspWindowStatus windowStatus=0;
+
+#ifdef NR_TDD
+ totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
+#endif
+ k0K1InfoTbl = &cell->cellCfg.schInitialDlBwp.k0K1InfoTbl;
+ if(cell->raReq[ueId-1]->isCFRA == false)
+ {
+ msg3K2InfoTbl = &cell->cellCfg.schInitialUlBwp.msg3K2InfoTbl;
+ puschMu = cell->cellCfg.numerology;
+ msg3Delta = puschDeltaTable[puschMu];
+ msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
+ }
+
+ /* Calculating time frame to send DCI for RAR */
+ ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA);
+ dciSlot = dciTime.slot;
+#ifdef NR_TDD
+ /* Consider this slot for sending DCI, only if it is a DL slot */
+ if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
+#endif
+ {
+ /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
+ if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
+ return false;
+
+ /* Check if this slot is within RA response window */
+ windowStatus = isInRaRspWindow(cell->raReq[ueId-1], dciTime, cell->numSlots);
+ if(windowStatus == WITHIN_WINDOW)
+ {
+ /* For all k0 values, search for a suitable k2 value to schedule MSG3.
+ * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
+ * is found. Else no scheduling happens.
+ */
+ for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
+ {
+ k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
+ k0 = cell->cellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
+
+ /* Calculating time frame to send RAR PDSCH */
+ ADD_DELTA_TO_TIME(dciTime, rarTime, k0);
+ rarSlot = rarTime.slot;
+
+ /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
+ if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
+ continue;
+
+ /* If Contention-FREE RA is in progress, allocate resources for
+ * PUCCH for next UL message */
+ if(cell->raReq[ueId-1]->isCFRA)
+ {
+ numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
+ for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
+ {
+ k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
+ if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg.dlDataToUlAck)
+ {
+ k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg.dlDataToUlAck->\
+ dlDataToUlAckList[k1Index];
+ }
+ else
+ {
+ k1 = defaultUlAckTbl[k1Index];
+ }
+
+ ADD_DELTA_TO_TIME(rarTime, pucchTime, k1);
+#ifdef NR_TDD
+ if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
+ continue;
+#endif
+ if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
+ continue;
+ k1Found = true;
+ break;
+ }
+ }
+ else
+ {
+ /* Else if contention-based RA is in progress, allocate resources for MSG3 */
+ for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
+ {
+ k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
+ k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
+
+ /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
+ k2 = k2 + msg3Delta;
+ if(k2 >= msg3MinSchTime)
+ {
+ ADD_DELTA_TO_TIME(rarTime, msg3Time, k2);
+#ifdef NR_TDD
+ if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
+ continue;
+#endif
+ /* If PUSCH is already scheduled on this slot, another PUSCH
+ * pdu cannot be scheduled here */
+ if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
+ continue;
+
+ k2Found = true;
+ break;
+ }
+ }
+ }
+ if(k1Found || k2Found)
+ break;
+ }
+ }
+ else if(windowStatus == WINDOW_EXPIRED)
+ {
+ SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
+ SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
+ return false;
+ }
+
+ /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
+ if(!k1Found && !k2Found)
+ return false;
+
+ /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
+ SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
+ if(dciSlotAlloc == NULLP)
+ {
+ DU_LOG("\nERROR --> SCH : Memory Allocation failed for dciSlotAlloc");
+ return false;
+ }
+ cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
+
+ /* Fill PDCCH and PDSCH scheduling information for RAR */
+ if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
+ {
+ DU_LOG("\nERROR --> SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
+ SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
+ cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
+ return false;
+ }
+
+ /* Fill RAR info */
+ dciSlotAlloc->rarInfo.raRnti = cell->raReq[ueId-1]->raRnti;
+ dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueId-1]->rachInd->crnti;
+ dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueId-1]->rachInd->preambleIdx;
+ dciSlotAlloc->rarInfo.ta = cell->raReq[ueId-1]->rachInd->timingAdv;
+
+ if(cell->raReq[ueId-1]->isCFRA)
+ {
+ /* Allocate resources for PUCCH */
+ schAllocPucchResource(cell, pucchTime, cell->raReq[ueId-1]->rachInd->crnti);
+ }
+ else
+ {
+ /* Allocate resources for msg3 */
+ msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time);
+ if(msg3PuschInfo)
+ {
+ dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb;
+ /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
+ dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
+ dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.startPrb;
+ dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.numPrb;
+ dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
+ dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
+ dciSlotAlloc->rarInfo.ulGrant.tpc = 3; /* TODO : Check appropriate value to be filled */
+ /* Spec 38.213, section 8.2 : In a contention based random access
+ * procedure, the CSI request field is reserved. */
+ dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
+ }
+ }
+
+ /* Check if both DCI and RAR are sent in the same slot.
+ * If not, allocate memory RAR PDSCH slot to store RAR info
+ */
+ if(dciSlot == rarSlot)
+ dciSlotAlloc->pduPres = BOTH;
+ else
+ {
+ /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
+ SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
+ if(rarSlotAlloc == NULLP)
+ {
+ DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarSlotAlloc");
+ SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
+ cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
+ return false;
+ }
+ cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
+
+ /* Copy all RAR info */
+ memcpy(rarSlotAlloc, dciSlotAlloc, sizeof(RarAlloc));
+ rarSlotAlloc->rarPdcchCfg.dci.pdschCfg = &rarSlotAlloc->rarPdschCfg;
+
+ /* Assign correct PDU types in corresponding slots */
+ rarSlotAlloc->pduPres = PDSCH_PDU;
+ dciSlotAlloc->pduPres = PDCCH_PDU;
+ dciSlotAlloc->pdschSlot = rarSlot;
+ }
+
+ cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
+ cell->schDlSlotInfo[rarSlot]->pdschUe = ueId;
+ if(cell->raReq[ueId-1]->isCFRA)
+ cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
+ else
+ cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
+
+ /* Create raCb at SCH */
+ createSchRaCb(cell->raReq[ueId-1], schInst);
+
+ /* Remove RachInd from pending RA request list */
+ SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
+ SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
+
+ return true;
+ }
+ return false;