1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /************************************************************************
25 Desc: C source code for rach handling functions
29 **********************************************************************/
32 @brief This file implements the rach handling.
34 #include "common_def.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
43 #include "sch_utils.h"
46 * @brief Checks if PRACH can be scheduled in current slot
50 * Function : schCheckPrachOcc
52 * This function checks if PRACH can be scheduled in
60 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
62 uint8_t prachCfgIdx = 0;
66 uint16_t prachSubframe = 0;
68 prachCfgIdx = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx;
70 /* derive the prachCfgIdx table paramters */
71 x = prachCfgIdxTable[prachCfgIdx][1];
72 y = prachCfgIdxTable[prachCfgIdx][2];
73 prachSubframe = prachCfgIdxTable[prachCfgIdx][3];
75 if((prachOccasionTimingInfo.sfn%x) == y)
77 subFrame = prachOccasionTimingInfo.slot/pow(2, cell->cellCfg.numerology);
79 /* check for subFrame number */
80 if ((1 << subFrame) & prachSubframe)
82 /* prach ocassion present in this subframe */
84 if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
85 cell->slotFrmtBitMap))
87 DU_LOG("\nERROR --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
98 * @brief Calculate number of PRBs to be allocated for PRACH
102 * Function : schCalcPrachNumRb
104 * Calculate number of PRBs to be allocated for PRACH
106 * @param[in] SchCellCb *cell, cell cb
107 * @return Number of PRBs
109 uint8_t schCalcPrachNumRb(SchCellCb *cell)
111 uint8_t tableIdx = 0;
112 uint16_t puschScs = convertScsEnumValToScsVal(cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.scs);
114 for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
116 if((numRbForPrachTable[tableIdx][0] == cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.rootSeqLen) &&
117 (numRbForPrachTable[tableIdx][1] == cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.msg1SubcSpacing) &&
118 (numRbForPrachTable[tableIdx][2] == puschScs))
120 return numRbForPrachTable[tableIdx][3];
127 * @brief resource allocation for PRACH
131 * Function : schPrachResAlloc
133 * This function handles PRACH allocation
135 * @param[in] SchCellCb *cell, cell cb
136 * @param[in] UlSchedInfo *ulSchedInfo, UL scheduling info
139 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
141 uint8_t numPrachRb = 0;
143 uint8_t prachCfgIdx = 0;
144 uint8_t prachFormat = 0;
145 uint8_t prachStartSymbol = 0;
146 uint8_t prachDuration = 0;
147 uint8_t prachOcas = 0;
148 uint8_t dataType = 0;
149 uint16_t freqStart = 0;
153 DU_LOG("\nERROR --> SCH : schPrachResAlloc(): Received cellCb is null");
157 /* If this slot is not a PRACH occassion, return */
158 if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
161 prachCfgIdx = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx;
162 prachFormat = prachCfgIdxTable[prachCfgIdx][0];
163 prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
164 prachOcas = prachCfgIdxTable[prachCfgIdx][6];
165 prachDuration = prachCfgIdxTable[prachCfgIdx][7];
167 /* numRa determined as 𝑛 belonging {0,1,.., M − 1},
168 * where M is given by msg1Fdm */
169 numRa = (cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1Fdm - 1);
171 /* freq domain resource determination for RACH*/
172 freqStart = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1FreqStart;
173 numPrachRb = schCalcPrachNumRb(cell);
174 /* Allocate PRACH resources from the UL resource bitmap */
175 allocatePrbUl(cell, prachOccasionTimingInfo, prachStartSymbol, prachDuration, &freqStart, numPrachRb);
178 dataType |= SCH_DATATYPE_PRACH;
179 ulSchedInfo->dataType = dataType;
180 ulSchedInfo->prachSchInfo.numPrachOcas = prachOcas;
181 ulSchedInfo->prachSchInfo.prachFormat = prachFormat;
182 ulSchedInfo->prachSchInfo.numRa = numRa;
183 ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
184 DU_LOG("\nINFO --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
188 * @brief Process RACH resource request for CFRA
192 * Function : SchProcRachRsrcReq
194 * This function processes RACH resorce request
195 * from MAC for CFRA. It assigns a dedicated preamble
196 * to the UE and sends the same in RACH resource
199 * @param[in] Post structure
200 * @param[in] RACH resource request
204 uint8_t SchProcRachRsrcReq(Pst *pst, SchRachRsrcReq *schRachRsrcReq)
206 uint8_t ssbIdx = 0, cfraSsbIdx = 0;
207 uint8_t firstCFPreambleIndex = 0, lastCFPreambleIndex = 0;
208 uint16_t cellIdx = 0;
211 Inst inst = pst->dstInst - SCH_INST_START;
212 SchCellCb *cellCb = NULLP;
213 SchUeCb *ueCb = NULLP;
214 SchRachRsrcRsp *rachRsrcRsp = NULLP;
216 DU_LOG("\nINFO --> SCH : Received RACH resource request for Cell ID [%d] CRNTI [%d]", \
217 schRachRsrcReq->cellId, schRachRsrcReq->crnti);
219 /* Fill RACH resource response to MAC */
220 SCH_ALLOC(rachRsrcRsp, sizeof(SchRachRsrcRsp));
223 DU_LOG("\nERROR --> SCH : Memory allocation failed for RACH resource response");
226 rachRsrcRsp->cellId = schRachRsrcReq->cellId;
227 rachRsrcRsp->crnti = schRachRsrcReq->crnti;
228 rachRsrcRsp->result = RSP_OK;
230 /* Fill SCH to MAC Pst structure */
231 memset(&rspPst, 0, sizeof(Pst));
232 FILL_PST_SCH_TO_MAC(rspPst, inst);
233 rspPst.event = EVENT_RACH_RESOURCE_RESPONSE_TO_MAC;
236 for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
238 if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcReq->cellId))
240 cellCb = schCb[inst].cells[cellIdx];
248 ueCb = schGetUeCb(cellCb, schRachRsrcReq->crnti);
249 if(ueCb->crnti != schRachRsrcReq->crnti)
251 DU_LOG("\nERROR --> SCH : CRNTI [%d] not found" ,schRachRsrcReq->crnti);
252 rachRsrcRsp->result = RSP_NOK;
257 DU_LOG("\nERROR --> SCH : Cell ID [%d] not found" ,schRachRsrcReq->cellId);
258 rachRsrcRsp->result = RSP_NOK;
261 /* Allocate SSB resource if no failure has occurred until this step */
262 if(rachRsrcRsp->result == RSP_OK)
264 /* Find first free preamble index from the pool CF preambles
265 * Preamble index from 0 to (numCbPreamblePerSsb-1) is used for CBRA
266 * Preamble index from numCbPreamblePerSsb to totalNumOfRAPreamble
267 * is used for CFRA */
268 firstCFPreambleIndex = cellCb->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.numCbPreamblePerSsb;
269 lastCFPreambleIndex = cellCb->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.totalNumRaPreamble;
271 /* Allocate resource for each SSB index requested */
272 for(ssbIdx = 0; ssbIdx < schRachRsrcReq->numSsb; ssbIdx++)
274 /* Find the first CF Preamble index not dedicated to any UE currently */
275 while(firstCFPreambleIndex <= lastCFPreambleIndex)
277 mask = 1 << firstCFPreambleIndex;
278 if(cellCb->dedPreambleBitMap & mask)
280 firstCFPreambleIndex++;
287 /* If firstCFPreambleIndex > lastCFPreambleIndex, it means all
288 * dedicated preambles are in use currently. In such a case, CBRA
289 * should be initiated.
290 * If a dedicated preamble is found, use this for CFRA and mark it as
291 * IN-USE in the bitmap.
292 * Considering only CFRA scenario for now. */
293 if(firstCFPreambleIndex <= lastCFPreambleIndex)
295 ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx = schRachRsrcReq->ssbIdx[ssbIdx];
296 ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx = firstCFPreambleIndex;
297 SET_ONE_BIT(firstCFPreambleIndex, cellCb->dedPreambleBitMap);
299 firstCFPreambleIndex++;
303 DU_LOG("\nINFO : SCH : No dedicated preameble availble to assign to ssbIdx[%d]", schRachRsrcReq->ssbIdx[ssbIdx]);
304 /* Breaking out of for loop since no dedicated preambles are available
305 * for remaining ssbIdx too */
310 ueCb->cfraResource.numSsb = cfraSsbIdx;
312 if(ueCb->cfraResource.numSsb == 0)
314 /* If numSsb is 0, it means no CFRA resource was alloacted for any of the
315 * SSB Idx, hence send a negative response */
316 rachRsrcRsp->result = RSP_NOK;
320 /* Send ssb resource information to MAC in RACH resource response */
321 rachRsrcRsp->cfraResource.numSsb = ueCb->cfraResource.numSsb;
322 memcpy(rachRsrcRsp->cfraResource.ssbResource, ueCb->cfraResource.ssbResource, \
323 ueCb->cfraResource.numSsb * sizeof(SchCfraSsbResource));
327 /* Free RACH resource request memory allocated by MAC */
328 SCH_FREE(schRachRsrcReq, sizeof(SchRachRsrcReq));
330 /* Send RACH resource response to MAC */
331 return(MacMessageRouter(&rspPst, (void *)rachRsrcRsp));
335 * @brief calculate ra-rnti function.
339 * Function : calculateRaRnti
341 * This function calculates ra-rnti
343 * @param[in] symbol index
344 * @param[in] slot index
345 * @param[in] frequency index
348 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
351 uint8_t ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
353 /* Refer to spec 38.321, section 5.1.3 */
354 raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
359 * @brief create raCb function.
363 * Function : createSchRaCb
365 * This function create raCb
368 * @param[in] shed instance
371 void createSchRaCb(uint8_t ueId, SchRaReq *raReq, Inst schInst)
375 /* If a UE in handover has triggered CFRA, its UE CB context is already present in SCH,
376 * Hence, no need to create raCb */
377 if(raReq->ueCb && (raReq->ueCb->state == SCH_UE_HANDIN_IN_PROGRESS))
379 schCb[schInst].cells[schInst]->numActvUe++;
380 SET_ONE_BIT(raReq->ueCb->ueId, schCb[schInst].cells[schInst]->actvUeBitMap);
381 raReq->ueCb->state = SCH_UE_STATE_ACTIVE;
382 schCb[schInst].cells[schInst]->raCb[ueId -1].raState = SCH_RA_STATE_MSG4_DONE;
387 /* Create RA CB only for CB-RA to use for msg3 and msg4 processing */
388 GET_UE_ID(raReq->rachInd->crnti, ueId);
389 schCb[schInst].cells[schInst]->raCb[ueId -1].tcrnti = raReq->rachInd->crnti;
390 schCb[schInst].cells[schInst]->raCb[ueId -1].msg4recvd = FALSE;
391 schCb[schInst].cells[schInst]->raCb[ueId -1].raState = SCH_RA_STATE_MSG3_PENDING;
393 schCb[schInst].cells[schInst]->raCb[ueId -1].cell = schCb[schInst].cells[schInst];
397 * @brief resource allocation for msg3 PUSCH
401 * Function : schAllocMsg3Pusch
403 * This function handles msg3 PUSCH allocation
405 * @param[in] Inst schInst, SCH instance
406 * @param[in] slot, current slot
407 * @param[out] msg3StartRb
408 * @param[out] msg3NumRb
411 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime, SchUlHqProcCb* msg3HqProc, bool isRetx)
413 SchCellCb *cell = NULLP;
414 SchUlSlotInfo *schUlSlotInfo = NULLP;
415 uint8_t mcs = DEFAULT_MCS;
416 uint8_t startSymb = 0;
418 uint16_t startRb = 0;
422 cell = schCb[schInst].cells[schInst];
425 DU_LOG("\n\nERROR --> SCH : Failed to find cell in schAllocMsg3Pusch");
429 /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
430 startSymb = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
431 symbLen = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
433 startRb = MAX_NUM_RB;
434 tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
435 numRb = schCalcNumPrb(tbSize, mcs, symbLen);
436 numRb++; /* allocating 1 extra RB for now */
437 allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
439 /* Fill PUSCH scheduling details in Slot structure */
440 schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
441 SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
442 if(!schUlSlotInfo->schPuschInfo)
444 DU_LOG("\nERROR --> SCH : Memory allocation failed in schAllocMsg3Pusch");
448 tbSize = 0; /* since nPrb has been incremented, recalculating tbSize */
449 tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
450 tbSize = tbSize / 8 ; /*bits to byte conversion*/
452 schUlSlotInfo->schPuschInfo->harqProcId = msg3HqProc->procId;
453 schUlSlotInfo->schPuschInfo->fdAlloc.resAllocType = SCH_ALLOC_TYPE_1;
454 schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.startPrb = startRb;
455 schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.numPrb = numRb;
456 schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
457 schUlSlotInfo->schPuschInfo->tdAlloc.numSymb = symbLen;
458 schUlSlotInfo->schPuschInfo->tbInfo.qamOrder = QPSK_MODULATION; /* QPSK modulation */
459 schUlSlotInfo->schPuschInfo->tbInfo.mcs = mcs;
460 schUlSlotInfo->schPuschInfo->tbInfo.mcsTable = SCH_MCS_TABLE_QAM_64;
461 schUlSlotInfo->schPuschInfo->tbInfo.ndi = NEW_TRANSMISSION; /* new transmission */
462 schUlSlotInfo->schPuschInfo->tbInfo.rv = 0;
463 schUlSlotInfo->schPuschInfo->tbInfo.tbSize = tbSize;
465 schUlSlotInfo->schPuschInfo->dmrsMappingType = DMRS_MAP_TYPE_A; /* Setting Type-A */
466 schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
467 schUlSlotInfo->schPuschInfo->dmrsAddPos = DMRS_ADDITIONAL_POS;
472 msg3HqProc->strtSymbl = startSymb;
473 msg3HqProc->numSymbl = symbLen;
474 msg3HqProc->puschResType = schUlSlotInfo->schPuschInfo->fdAlloc.resAllocType;
475 msg3HqProc->puschStartPrb = schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.startPrb;
476 msg3HqProc->puschNumPrb = schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.numPrb;
477 msg3HqProc->tbInfo.qamOrder = schUlSlotInfo->schPuschInfo->tbInfo.qamOrder;
478 msg3HqProc->tbInfo.iMcs = schUlSlotInfo->schPuschInfo->tbInfo.mcs;
479 msg3HqProc->tbInfo.mcsTable = schUlSlotInfo->schPuschInfo->tbInfo.mcsTable;
480 msg3HqProc->tbInfo.ndi = schUlSlotInfo->schPuschInfo->tbInfo.ndi;
481 msg3HqProc->tbInfo.rv = schUlSlotInfo->schPuschInfo->tbInfo.rv;
482 msg3HqProc->tbInfo.tbSzReq = schUlSlotInfo->schPuschInfo->tbInfo.tbSize;
484 msg3HqProc->dmrsMappingType = schUlSlotInfo->schPuschInfo->dmrsMappingType;
485 msg3HqProc->nrOfDmrsSymbols = schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols;
486 msg3HqProc->dmrsAddPos = schUlSlotInfo->schPuschInfo->dmrsAddPos;
489 return schUlSlotInfo->schPuschInfo;
493 * @brief Check if a time frame is in RA Response window
497 * Function : isInRaRspWindow
499 * Check if a time frame is in RA Response window
501 * @param[in] RA request
502 * @param[in] Time frame to check
503 * @param[in] Total number of slot per radio frame
507 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
509 uint32_t winStartTime, winEndTime, timeToCheck;
511 winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
512 winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
513 timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
515 /* TODO : check how to handle the wrap around scenario of MAX_SFN */
516 if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
517 return WITHIN_WINDOW;
518 else if(timeToCheck < winStartTime)
519 return WINDOW_YET_TO_START;
521 return WINDOW_EXPIRED;
525 * @brief Processes any pending RA request
529 * Function : schProcessRaReq
531 * This function process pending RA request
533 * @param[in] Current timing of the cell
536 bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
538 bool k1Found = false, k2Found = false;
539 uint8_t k0TblIdx = 0, k1TblIdx = 0, k2TblIdx = 0;
540 uint8_t k0Index = 0, k1Index = 0, k2Index = 0;
541 uint8_t k0 = 0, k1 = 0, k2 = 0;
544 uint8_t msg3Delta = 0, msg3MinSchTime = 0;
546 uint8_t totalCfgSlot = 0;
548 uint16_t dciSlot = 0, rarSlot = 0;
549 SlotTimingInfo dciTime, rarTime, msg3Time, pucchTime;
550 RarAlloc *dciSlotAlloc = NULLP; /* Stores info for transmission of PDCCH for RAR */
551 RarAlloc *rarSlotAlloc = NULLP; /* Stores info for transmission of RAR PDSCH */
552 SchPuschInfo *msg3PuschInfo = NULLP; /* Stores MSG3 PUSCH scheduling information */
553 SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;
554 SchK2TimingInfoTbl *msg3K2InfoTbl=NULLP;
555 RaRspWindowStatus windowStatus=0;
558 totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
560 k0K1InfoTbl = &cell->k0K1InfoTbl;
561 if(cell->raReq[ueId-1]->isCFRA == false)
563 msg3K2InfoTbl = &cell->msg3K2InfoTbl;
564 puschMu = cell->cellCfg.numerology;
565 msg3Delta = puschDeltaTable[puschMu];
566 msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
569 /* Calculating time frame to send DCI for RAR */
570 ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
571 dciSlot = dciTime.slot;
573 /* Consider this slot for sending DCI, only if it is a DL slot */
574 if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
577 /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
578 if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
581 /* Check if this slot is within RA response window */
582 windowStatus = isInRaRspWindow(cell->raReq[ueId-1], dciTime, cell->numSlots);
583 if(windowStatus == WITHIN_WINDOW)
585 /* For all k0 values, search for a suitable k2 value to schedule MSG3.
586 * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
587 * is found. Else no scheduling happens.
589 for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
591 k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
592 k0 = cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
594 /* Calculating time frame to send RAR PDSCH */
595 ADD_DELTA_TO_TIME(dciTime, rarTime, k0, cell->numSlots);
596 rarSlot = rarTime.slot;
598 /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
599 if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
602 /* If Contention-FREE RA is in progress, allocate resources for
603 * PUCCH for next UL message */
604 if(cell->raReq[ueId-1]->isCFRA)
606 numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
607 for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
609 k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
610 if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck)
612 k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck->\
613 dlDataToUlAckList[k1Index];
617 k1 = defaultUlAckTbl[k1Index];
620 ADD_DELTA_TO_TIME(rarTime, pucchTime, k1, cell->numSlots);
622 if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
625 if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
633 /* Else if contention-based RA is in progress, allocate resources for MSG3 */
634 for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
636 k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
637 k2 = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
639 /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
641 if(k2 >= msg3MinSchTime)
643 ADD_DELTA_TO_TIME(rarTime, msg3Time, k2, cell->numSlots);
645 if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
648 /* If PUSCH is already scheduled on this slot, another PUSCH
649 * pdu cannot be scheduled here */
650 if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
658 if(k1Found || k2Found)
662 else if(windowStatus == WINDOW_EXPIRED)
664 SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
665 SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
669 /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
670 if(!k1Found && !k2Found)
673 /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
674 SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
675 if(dciSlotAlloc == NULLP)
677 DU_LOG("\nERROR --> SCH : Memory Allocation failed for dciSlotAlloc");
680 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
682 /* Fill PDCCH and PDSCH scheduling information for RAR */
683 if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
685 DU_LOG("\nERROR --> SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
686 if(!dciSlotAlloc->rarPdschCfg)
688 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
689 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
695 dciSlotAlloc->raRnti = cell->raReq[ueId-1]->raRnti;
696 dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueId-1]->rachInd->crnti;
697 dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueId-1]->rachInd->preambleIdx;
698 dciSlotAlloc->rarInfo.ta = cell->raReq[ueId-1]->rachInd->timingAdv;
700 if(cell->raReq[ueId-1]->isCFRA)
702 /* Allocate resources for PUCCH */
703 schAllocPucchResource(cell, pucchTime, cell->raReq[ueId-1]->rachInd->crnti,NULLP, FALSE, NULLP);
707 /* Allocate resources for msg3 */
708 msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time, &(cell->raCb[ueId-1].msg3HqProc), FALSE);
711 dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.freqAlloc.numPrb;
712 /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
713 dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
714 dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.startPrb;
715 dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.numPrb;
716 dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
717 dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
718 dciSlotAlloc->rarInfo.ulGrant.tpc = 3; /* TODO : Check appropriate value to be filled */
719 /* Spec 38.213, section 8.2 : In a contention based random access
720 * procedure, the CSI request field is reserved. */
721 dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
725 /* Check if both DCI and RAR are sent in the same slot.
726 * If not, allocate memory RAR PDSCH slot to store RAR info
728 if(dciSlot == rarSlot)
730 SCH_ALLOC(dciSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
731 if(dciSlotAlloc->rarPdschCfg)
733 memcpy(dciSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci.pdschCfg, sizeof(PdschCfg));
737 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
738 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
739 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
740 DU_LOG("\nERROR --> SCH : Memory Allocation failed for dciSlotAlloc->rarPdschCfg");
747 /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
748 SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
749 if(rarSlotAlloc == NULLP)
751 DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarSlotAlloc");
752 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
753 if(!dciSlotAlloc->rarPdschCfg)
755 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
756 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
760 cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
762 /* Copy all RAR info */
763 rarSlotAlloc->raRnti = dciSlotAlloc->raRnti;
764 rarSlotAlloc->bwp = dciSlotAlloc->bwp;
765 SCH_ALLOC(rarSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
766 if(rarSlotAlloc->rarPdschCfg)
768 memcpy(rarSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci.pdschCfg,sizeof(PdschCfg));
772 DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarSlotAlloc->rarPdschCfg");
773 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
774 if(!dciSlotAlloc->rarPdschCfg)
776 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
777 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
779 SCH_FREE(rarSlotAlloc, sizeof(RarAlloc));
780 cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = NULLP;
785 cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
786 cell->schDlSlotInfo[rarSlot]->pdschUe = ueId;
787 if(cell->raReq[ueId-1]->isCFRA)
788 cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
790 cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
792 /* Create raCb at SCH */
793 createSchRaCb(ueId, cell->raReq[ueId-1], schInst);
795 /* Remove RachInd from pending RA request list */
796 SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
797 SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
805 * @brief process rach indication function.
809 * Function : SchProcRachInd
811 * This function process rach indication
813 * @param[in] rachInd parameters
814 * @param[in] shed instance
817 uint8_t SchProcRachInd(Pst *pst, RachIndInfo *rachInd)
819 SchRaReq *raReq = NULLP;
823 Inst schInst = pst->dstInst-SCH_INST_START;
824 SchCellCb *cell = schCb[schInst].cells[schInst];
826 DU_LOG("\nINFO --> SCH : Received Rach indication");
830 DU_LOG("\nERROR --> SCH: Failed to find cell in SchProcRachInd");
834 /* Storing RA request in cellCb */
835 GET_UE_ID(rachInd->crnti, ueId);
838 DU_LOG("\nERROR --> SCH: Invalid CRNTI [%d]", rachInd->crnti);
842 SCH_ALLOC(raReq, sizeof(SchRaReq));
845 DU_LOG("\nERROR --> SCH : Memory allocation failure in SchProcRachInd");
846 SCH_FREE(rachInd, sizeof(RachIndInfo));
850 /* calculate the ra-rnti value */
851 raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
852 raReq->rachInd = rachInd;
853 if((cell->ueCb[ueId-1].crnti == rachInd->crnti) && (cell->ueCb[ueId-1].state == SCH_UE_HANDIN_IN_PROGRESS))
855 raReq->isCFRA = true;
856 raReq->ueCb = &cell->ueCb[ueId-1];
859 raReq->isCFRA = false;
860 raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
861 raReq->winStartTime.slot = rachInd->timingInfo.slot;
863 /* Converting window size from ms to number of slots */
864 slotDuration = (1 / pow(2, cell->cellCfg.numerology));
865 winNumSlots = (float)cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.raRspWindow / slotDuration;
867 /* Adding window size to window start time to get window end time */
868 ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots, cell->numSlots);
869 cell->raReq[ueId -1] = raReq;
871 /* Adding UE Id to list of pending UEs to be scheduled */
872 cell->api->SchRachInd(cell, ueId);
877 * @brief fill RAR info function.
881 * Function : calculateRaRnti
883 * This function fills pdcch and pdsch info for RAR
885 * @param[in] rar Allocation info
888 * @param[in] offset to pointA to determine freq alloc
891 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
893 uint8_t coreset0Idx = 0;
894 uint8_t firstSymbol = 0, numSymbols = 0;
895 uint8_t mcs = DEFAULT_MCS; /* MCS fixed to 4 */
896 uint8_t dmrsStartSymbol, startSymbol, numSymbol ;
901 SchBwpDlCfg *initialBwp = &cell->cellCfg.dlCfgCommon.schInitialDlBwp;
902 SCH_ALLOC(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
903 if(rarAlloc->rarPdcchCfg == NULLP)
905 DU_LOG("\nERROR --> SCH : Memory allocation failed in %s",__func__);
908 PdcchCfg *pdcch = rarAlloc->rarPdcchCfg;
909 BwpCfg *bwp = &rarAlloc->bwp;
911 /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
912 coreset0Idx = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
913 numRbs = coresetIdxTable[coreset0Idx][1];
914 numSymbols = coresetIdxTable[coreset0Idx][2];
916 /* calculate time domain parameters */
917 // note: since slot value is made sl1, RAR can be sent at all slots
918 uint16_t mask = 0x2000;
919 for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
921 if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
928 bwp->freqAlloc.numPrb = initialBwp->bwp.freqAlloc.numPrb;
929 bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
930 bwp->subcarrierSpacing = initialBwp->bwp.scs;
931 bwp->cyclicPrefix = initialBwp->bwp.cyclicPrefix;
933 /* fill the PDCCH PDU */
934 pdcch->coresetCfg.startSymbolIndex = firstSymbol;
935 pdcch->coresetCfg.durationSymbols = numSymbols;
936 memcpy(pdcch->coresetCfg.freqDomainResource, \
937 cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
939 pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
940 pdcch->coresetCfg.regBundleSize = 6; /* spec-38.211 sec 7.3.2.2 */
941 pdcch->coresetCfg.interleaverSize = 2; /* spec-38.211 sec 7.3.2.2 */
942 pdcch->coresetCfg.coreSetType = 0;
943 pdcch->coresetCfg.coreSetSize = numRbs;
944 pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
945 pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
947 pdcch->dci.rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
948 pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
949 pdcch->dci.scramblingRnti = 0;
950 pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
951 pdcch->dci.aggregLevel = 4;
952 pdcch->dci.beamPdcchInfo.numPrgs = 1;
953 pdcch->dci.beamPdcchInfo.prgSize = 1;
954 pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
955 pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
956 pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
957 pdcch->dci.txPdcchPower.beta_pdcch_1_0 = 0;
958 pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
960 pdsch = &pdcch->dci.pdschCfg;
961 /* fill the PDSCH PDU */
963 pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
964 pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
966 pdsch->numCodewords = 1;
967 for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
969 pdsch->codeword[cwCount].targetCodeRate = 308;
970 pdsch->codeword[cwCount].qamModOrder = 2;
971 pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
972 pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
973 pdsch->codeword[cwCount].rvIndex = 0;
974 /* RAR PDU length and FAPI payload header length */
975 tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
976 pdsch->codeword[cwCount].tbSize = tbSize;
978 pdsch->dataScramblingId = cell->cellCfg.phyCellId;
979 pdsch->numLayers = 1;
980 pdsch->transmissionScheme = 0;
982 pdsch->dmrs.dlDmrsSymbPos = DL_DMRS_SYMBOL_POS;
983 pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
984 pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
985 pdsch->dmrs.scid = 0;
986 pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
987 pdsch->dmrs.dmrsPorts = 0;
988 pdsch->dmrs.mappingType = DMRS_MAP_TYPE_A; /* Type-A */
989 pdsch->dmrs.nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
990 pdsch->dmrs.dmrsAddPos = DMRS_ADDITIONAL_POS;
992 pdsch->pdschTimeAlloc.rowIndex = k0Index;
993 pdsch->pdschTimeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
994 pdsch->pdschTimeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
996 pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
997 pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
998 pdsch->pdschFreqAlloc.startPrb = MAX_NUM_RB;
999 pdsch->pdschFreqAlloc.numPrb = \
1000 schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
1002 /* Find total symbols occupied including DMRS */
1003 dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
1004 /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
1005 * in that case only PDSCH symbols are marked as occupied */
1006 if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
1008 startSymbol = pdsch->pdschTimeAlloc.startSymb;
1009 numSymbol = pdsch->pdschTimeAlloc.numSymb;
1011 /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
1014 startSymbol = dmrsStartSymbol;
1015 numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
1018 /* Allocate the number of PRBs required for RAR PDSCH */
1019 if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
1020 &pdsch->pdschFreqAlloc.startPrb, pdsch->pdschFreqAlloc.numPrb)) != ROK)
1022 DU_LOG("\nERROR --> SCH : allocatePrbDl() failed for RAR");
1023 SCH_FREE(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
1027 pdsch->beamPdschInfo.numPrgs = 1;
1028 pdsch->beamPdschInfo.prgSize = 1;
1029 pdsch->beamPdschInfo.digBfInterfaces = 0;
1030 pdsch->beamPdschInfo.prg[0].pmIdx = 0;
1031 pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
1032 pdsch->txPdschPower.powerControlOffset = 0;
1033 pdsch->txPdschPower.powerControlOffsetSS = 0;
1038 /* @brief Process RACH resource release after CFRA
1042 * Function : SchProcRachRsrcRel
1044 * This function processes RACH resorce release
1045 * from MAC after CFRA. It releases the dedicated
1046 * preamble alloted to the UE
1048 * @param[in] Post structure
1049 * @param[in] RACH resource release
1053 uint8_t SchProcRachRsrcRel(Pst *pst, SchRachRsrcRel *schRachRsrcRel)
1056 uint8_t ssbIdx = 0, cfraSsbIdx = 0;
1057 uint16_t cellIdx = 0;
1058 Inst inst = pst->dstInst - SCH_INST_START;
1059 SchCellCb *cellCb = NULLP;
1060 SchUeCb *ueCb = NULLP;
1062 DU_LOG("\nINFO --> SCH : Received RACH resource release for Cell ID [%d] CRNTI [%d]", \
1063 schRachRsrcRel->cellId, schRachRsrcRel->crnti);
1066 for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1068 if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcRel->cellId))
1070 cellCb = schCb[inst].cells[cellIdx];
1078 ueCb = schGetUeCb(cellCb, schRachRsrcRel->crnti);
1079 if(ueCb->crnti != schRachRsrcRel->crnti)
1081 DU_LOG("\nERROR --> SCH : CRNTI [%d] not found", schRachRsrcRel->crnti);
1087 DU_LOG("\nERROR --> SCH : Cell ID [%d] not found", schRachRsrcRel->cellId);
1091 /* Free SSB resource if no failure has occurred until this step */
1094 for(ssbIdx = 0; ssbIdx < schRachRsrcRel->cfraResource.numSsb; ssbIdx++)
1096 /* Search each ssbIdx entry in UE Cb */
1097 for(cfraSsbIdx = 0; cfraSsbIdx < ueCb->cfraResource.numSsb; cfraSsbIdx++)
1099 if(ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx == schRachRsrcRel->cfraResource.ssbResource[ssbIdx].ssbIdx)
1101 /* If ssbIdx entry is found in UE CB, free dedicated resources
1102 * for this ssbIdx */
1103 UNSET_ONE_BIT(ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx, cellCb->dedPreambleBitMap);
1104 memset(&ueCb->cfraResource.ssbResource[cfraSsbIdx], 0, sizeof(SchCfraSsbResource));
1105 ueCb->cfraResource.numSsb--;
1112 /* Free RACH resource release memory allocated by MAC */
1113 SCH_FREE(schRachRsrcRel, sizeof(SchRachRsrcRel));
1116 /* @brief process MSG4 completion
1120 * Function : schMsg4Complete
1122 * This function updates ra state and msg4 Hqrq
1123 * proc upon MSG4 completion
1124 * @param[in] SchUeCb *ueCb, UE cb pointer
1127 void schMsg4Complete(SchUeCb *ueCb)
1129 DU_LOG("\nINFO --> SCH: State change for ueId[%2d] to SCH_RA_STATE_MSG4_DONE\n",ueCb->ueId);
1130 ueCb->cellCb->raCb[ueCb->ueId-1].raState = SCH_RA_STATE_MSG4_DONE;
1131 ueCb->msg4HqProc = ueCb->retxMsg4HqProc = NULLP;
1133 /**********************************************************************
1135 **********************************************************************/