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"
44 #include "sch_utils.h"
47 * @brief Checks if PRACH can be scheduled in current slot
51 * Function : schCheckPrachOcc
53 * This function checks if PRACH can be scheduled in
61 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
63 uint8_t prachCfgIdx = 0;
67 uint16_t prachSubframe = 0;
69 prachCfgIdx = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx;
71 /* derive the prachCfgIdx table paramters */
72 x = prachCfgIdxTable[prachCfgIdx][1];
73 y = prachCfgIdxTable[prachCfgIdx][2];
74 prachSubframe = prachCfgIdxTable[prachCfgIdx][3];
76 if((prachOccasionTimingInfo.sfn%x) == y)
78 subFrame = prachOccasionTimingInfo.slot/pow(2, cell->numerology);
80 /* check for subFrame number */
81 if ((1 << subFrame) & prachSubframe)
83 /* prach ocassion present in this subframe */
85 if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
86 cell->slotFrmtBitMap))
88 DU_LOG("\nERROR --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
99 * @brief Calculate number of PRBs to be allocated for PRACH
103 * Function : schCalcPrachNumRb
105 * Calculate number of PRBs to be allocated for PRACH
107 * @param[in] SchCellCb *cell, cell cb
108 * @return Number of PRBs
110 uint8_t schCalcPrachNumRb(SchCellCb *cell)
112 uint8_t tableIdx = 0;
113 uint16_t puschScs = convertScsEnumValToScsVal(cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.scs);
115 for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
117 if((numRbForPrachTable[tableIdx][0] == cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.rootSeqLen) &&
118 (numRbForPrachTable[tableIdx][1] == cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.msg1SubcSpacing) &&
119 (numRbForPrachTable[tableIdx][2] == puschScs))
121 return numRbForPrachTable[tableIdx][3];
128 * @brief resource allocation for PRACH
132 * Function : schPrachResAlloc
134 * This function handles PRACH allocation
136 * @param[in] SchCellCb *cell, cell cb
137 * @param[in] UlSchedInfo *ulSchedInfo, UL scheduling info
140 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
142 uint8_t numPrachRb = 0;
144 uint8_t prachCfgIdx = 0;
145 uint8_t prachFormat = 0;
146 uint8_t prachStartSymbol = 0;
147 uint8_t prachDuration = 0;
148 uint8_t prachOcas = 0;
149 uint8_t dataType = 0;
150 uint16_t freqStart = 0;
154 DU_LOG("\nERROR --> SCH : schPrachResAlloc(): Received cellCb is null");
158 /* If this slot is not a PRACH occassion, return */
159 if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
162 prachCfgIdx = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx;
163 prachFormat = prachCfgIdxTable[prachCfgIdx][0];
164 prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
165 prachOcas = prachCfgIdxTable[prachCfgIdx][6];
166 prachDuration = prachCfgIdxTable[prachCfgIdx][7];
168 /* numRa determined as 𝑛 belonging {0,1,.., M − 1},
169 * where M is given by msg1Fdm */
170 numRa = (cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1Fdm - 1);
172 /* freq domain resource determination for RACH*/
173 freqStart = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1FreqStart;
174 numPrachRb = schCalcPrachNumRb(cell);
175 /* Allocate PRACH resources from the UL resource bitmap */
176 allocatePrbUl(cell, prachOccasionTimingInfo, prachStartSymbol, prachDuration, &freqStart, numPrachRb);
179 dataType |= SCH_DATATYPE_PRACH;
180 ulSchedInfo->dataType = dataType;
181 ulSchedInfo->prachSchInfo.numPrachOcas = prachOcas;
182 ulSchedInfo->prachSchInfo.prachFormat = prachFormat;
183 ulSchedInfo->prachSchInfo.numRa = numRa;
184 ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
185 DU_LOG("\nINFO --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
189 * @brief Process RACH resource request for CFRA
193 * Function : SchProcRachRsrcReq
195 * This function processes RACH resorce request
196 * from MAC for CFRA. It assigns a dedicated preamble
197 * to the UE and sends the same in RACH resource
200 * @param[in] Post structure
201 * @param[in] RACH resource request
205 uint8_t SchProcRachRsrcReq(Pst *pst, SchRachRsrcReq *schRachRsrcReq)
207 uint8_t ssbIdx = 0, cfraSsbIdx = 0;
208 uint8_t firstCFPreambleIndex = 0, lastCFPreambleIndex = 0;
209 uint16_t cellIdx = 0;
212 Inst inst = pst->dstInst - SCH_INST_START;
213 SchCellCb *cellCb = NULLP;
214 SchUeCb *ueCb = NULLP;
215 SchRachRsrcRsp *rachRsrcRsp = NULLP;
217 DU_LOG("\nINFO --> SCH : Received RACH resource request for Cell ID [%d] CRNTI [%d]", \
218 schRachRsrcReq->cellId, schRachRsrcReq->crnti);
220 /* Fill RACH resource response to MAC */
221 SCH_ALLOC(rachRsrcRsp, sizeof(SchRachRsrcRsp));
224 DU_LOG("\nERROR --> SCH : Memory allocation failed for RACH resource response");
227 rachRsrcRsp->cellId = schRachRsrcReq->cellId;
228 rachRsrcRsp->crnti = schRachRsrcReq->crnti;
229 rachRsrcRsp->result = RSP_OK;
231 /* Fill SCH to MAC Pst structure */
232 memset(&rspPst, 0, sizeof(Pst));
233 FILL_PST_SCH_TO_MAC(rspPst, inst);
234 rspPst.event = EVENT_RACH_RESOURCE_RESPONSE_TO_MAC;
237 for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
239 if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcReq->cellId))
241 cellCb = schCb[inst].cells[cellIdx];
249 ueCb = schGetUeCb(cellCb, schRachRsrcReq->crnti);
250 if(ueCb->crnti != schRachRsrcReq->crnti)
252 DU_LOG("\nERROR --> SCH : CRNTI [%d] not found" ,schRachRsrcReq->crnti);
253 rachRsrcRsp->result = RSP_NOK;
258 DU_LOG("\nERROR --> SCH : Cell ID [%d] not found" ,schRachRsrcReq->cellId);
259 rachRsrcRsp->result = RSP_NOK;
262 /* Allocate SSB resource if no failure has occurred until this step */
263 if(rachRsrcRsp->result == RSP_OK)
265 /* Find first free preamble index from the pool CF preambles
266 * Preamble index from 0 to (numCbPreamblePerSsb-1) is used for CBRA
267 * Preamble index from numCbPreamblePerSsb to totalNumOfRAPreamble
268 * is used for CFRA */
269 firstCFPreambleIndex = cellCb->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.numCbPreamblePerSsb;
270 lastCFPreambleIndex = cellCb->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.totalNumRaPreamble;
272 /* Allocate resource for each SSB index requested */
273 for(ssbIdx = 0; ssbIdx < schRachRsrcReq->numSsb; ssbIdx++)
275 /* Find the first CF Preamble index not dedicated to any UE currently */
276 while(firstCFPreambleIndex <= lastCFPreambleIndex)
278 mask = 1 << firstCFPreambleIndex;
279 if(cellCb->dedPreambleBitMap & mask)
281 firstCFPreambleIndex++;
288 /* If firstCFPreambleIndex > lastCFPreambleIndex, it means all
289 * dedicated preambles are in use currently. In such a case, CBRA
290 * should be initiated.
291 * If a dedicated preamble is found, use this for CFRA and mark it as
292 * IN-USE in the bitmap.
293 * Considering only CFRA scenario for now. */
294 if(firstCFPreambleIndex <= lastCFPreambleIndex)
296 ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx = schRachRsrcReq->ssbIdx[ssbIdx];
297 ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx = firstCFPreambleIndex;
298 SET_ONE_BIT(firstCFPreambleIndex, cellCb->dedPreambleBitMap);
300 firstCFPreambleIndex++;
304 DU_LOG("\nINFO : SCH : No dedicated preameble availble to assign to ssbIdx[%d]", schRachRsrcReq->ssbIdx[ssbIdx]);
305 /* Breaking out of for loop since no dedicated preambles are available
306 * for remaining ssbIdx too */
311 ueCb->cfraResource.numSsb = cfraSsbIdx;
313 if(ueCb->cfraResource.numSsb == 0)
315 /* If numSsb is 0, it means no CFRA resource was alloacted for any of the
316 * SSB Idx, hence send a negative response */
317 rachRsrcRsp->result = RSP_NOK;
321 /* Send ssb resource information to MAC in RACH resource response */
322 rachRsrcRsp->cfraResource.numSsb = ueCb->cfraResource.numSsb;
323 memcpy(rachRsrcRsp->cfraResource.ssbResource, ueCb->cfraResource.ssbResource, \
324 ueCb->cfraResource.numSsb * sizeof(SchCfraSsbResource));
328 /* Free RACH resource request memory allocated by MAC */
329 SCH_FREE(schRachRsrcReq, sizeof(SchRachRsrcReq));
331 /* Send RACH resource response to MAC */
332 return(MacMessageRouter(&rspPst, (void *)rachRsrcRsp));
336 * @brief calculate ra-rnti function.
340 * Function : calculateRaRnti
342 * This function calculates ra-rnti
344 * @param[in] symbol index
345 * @param[in] slot index
346 * @param[in] frequency index
349 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
352 uint8_t ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
354 /* Refer to spec 38.321, section 5.1.3 */
355 raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
360 * @brief create raCb function.
364 * Function : createSchRaCb
366 * This function create raCb
369 * @param[in] shed instance
372 void createSchRaCb(uint8_t ueId, SchRaReq *raReq, Inst schInst)
376 /* If a UE in handover has triggered CFRA, its UE CB context is already present in SCH,
377 * Hence, no need to create raCb */
378 if(raReq->ueCb && (raReq->ueCb->state == SCH_UE_HANDIN_IN_PROGRESS))
380 schCb[schInst].cells[schInst]->numActvUe++;
381 SET_ONE_BIT(raReq->ueCb->ueId, schCb[schInst].cells[schInst]->actvUeBitMap);
382 raReq->ueCb->state = SCH_UE_STATE_ACTIVE;
383 schCb[schInst].cells[schInst]->raCb[ueId -1].raState = SCH_RA_STATE_MSG4_DONE;
388 /* Create RA CB only for CB-RA to use for msg3 and msg4 processing */
389 GET_UE_ID(raReq->rachInd->crnti, ueId);
390 schCb[schInst].cells[schInst]->raCb[ueId -1].tcrnti = raReq->rachInd->crnti;
391 schCb[schInst].cells[schInst]->raCb[ueId -1].msg4recvd = FALSE;
392 schCb[schInst].cells[schInst]->raCb[ueId -1].raState = SCH_RA_STATE_MSG3_PENDING;
394 schCb[schInst].cells[schInst]->raCb[ueId -1].cell = schCb[schInst].cells[schInst];
398 * @brief resource allocation for msg3 PUSCH
402 * Function : schAllocMsg3Pusch
404 * This function handles msg3 PUSCH allocation
406 * @param[in] Inst schInst, SCH instance
407 * @param[in] slot, current slot
408 * @param[out] msg3StartRb
409 * @param[out] msg3NumRb
412 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime, SchUlHqProcCb* msg3HqProc, bool isRetx)
414 SchCellCb *cell = NULLP;
415 SchUlSlotInfo *schUlSlotInfo = NULLP;
416 uint8_t mcs = DEFAULT_MCS;
417 uint8_t startSymb = 0;
419 uint16_t startRb = 0;
423 cell = schCb[schInst].cells[schInst];
426 DU_LOG("\n\nERROR --> SCH : Failed to find cell in schAllocMsg3Pusch");
430 /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
431 startSymb = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
432 symbLen = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
434 startRb = MAX_NUM_RB;
435 tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
436 numRb = schCalcNumPrb(tbSize, mcs, symbLen);
437 numRb++; /* allocating 1 extra RB for now */
438 allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
440 /* Fill PUSCH scheduling details in Slot structure */
441 schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
442 SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
443 if(!schUlSlotInfo->schPuschInfo)
445 DU_LOG("\nERROR --> SCH : Memory allocation failed in schAllocMsg3Pusch");
449 tbSize = 0; /* since nPrb has been incremented, recalculating tbSize */
450 tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
451 tbSize = tbSize / 8 ; /*bits to byte conversion*/
453 schUlSlotInfo->schPuschInfo->harqProcId = msg3HqProc->procId;
454 schUlSlotInfo->schPuschInfo->fdAlloc.resAllocType = SCH_ALLOC_TYPE_1;
455 schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.startPrb = startRb;
456 schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.numPrb = numRb;
457 schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
458 schUlSlotInfo->schPuschInfo->tdAlloc.numSymb = symbLen;
459 schUlSlotInfo->schPuschInfo->tbInfo.qamOrder = QPSK_MODULATION; /* QPSK modulation */
460 schUlSlotInfo->schPuschInfo->tbInfo.mcs = mcs;
461 schUlSlotInfo->schPuschInfo->tbInfo.mcsTable = SCH_MCS_TABLE_QAM_64;
462 schUlSlotInfo->schPuschInfo->tbInfo.ndi = NEW_TRANSMISSION; /* new transmission */
463 schUlSlotInfo->schPuschInfo->tbInfo.rv = 0;
464 schUlSlotInfo->schPuschInfo->tbInfo.tbSize = tbSize;
466 schUlSlotInfo->schPuschInfo->dmrsMappingType = DMRS_MAP_TYPE_A; /* Setting Type-A */
467 schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
468 schUlSlotInfo->schPuschInfo->dmrsAddPos = DMRS_ADDITIONAL_POS;
473 msg3HqProc->strtSymbl = startSymb;
474 msg3HqProc->numSymbl = symbLen;
475 msg3HqProc->puschResType = schUlSlotInfo->schPuschInfo->fdAlloc.resAllocType;
476 msg3HqProc->puschStartPrb = schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.startPrb;
477 msg3HqProc->puschNumPrb = schUlSlotInfo->schPuschInfo->fdAlloc.resAlloc.type1.numPrb;
478 msg3HqProc->tbInfo.qamOrder = schUlSlotInfo->schPuschInfo->tbInfo.qamOrder;
479 msg3HqProc->tbInfo.iMcs = schUlSlotInfo->schPuschInfo->tbInfo.mcs;
480 msg3HqProc->tbInfo.mcsTable = schUlSlotInfo->schPuschInfo->tbInfo.mcsTable;
481 msg3HqProc->tbInfo.ndi = schUlSlotInfo->schPuschInfo->tbInfo.ndi;
482 msg3HqProc->tbInfo.rv = schUlSlotInfo->schPuschInfo->tbInfo.rv;
483 msg3HqProc->tbInfo.tbSzReq = schUlSlotInfo->schPuschInfo->tbInfo.tbSize;
485 msg3HqProc->dmrsMappingType = schUlSlotInfo->schPuschInfo->dmrsMappingType;
486 msg3HqProc->nrOfDmrsSymbols = schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols;
487 msg3HqProc->dmrsAddPos = schUlSlotInfo->schPuschInfo->dmrsAddPos;
490 return schUlSlotInfo->schPuschInfo;
494 * @brief Check if a time frame is in RA Response window
498 * Function : isInRaRspWindow
500 * Check if a time frame is in RA Response window
502 * @param[in] RA request
503 * @param[in] Time frame to check
504 * @param[in] Total number of slot per radio frame
508 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
510 uint32_t winStartTime, winEndTime, timeToCheck;
512 winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
513 winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
514 timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
516 /* TODO : check how to handle the wrap around scenario of MAX_SFN */
517 if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
518 return WITHIN_WINDOW;
519 else if(timeToCheck < winStartTime)
520 return WINDOW_YET_TO_START;
522 return WINDOW_EXPIRED;
526 * @brief Processes any pending RA request
530 * Function : schProcessRaReq
532 * This function process pending RA request
534 * @param[in] Current timing of the cell
537 bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
539 bool k1Found = false, k2Found = false;
540 uint8_t k0TblIdx = 0, k1TblIdx = 0, k2TblIdx = 0;
541 uint8_t k0Index = 0, k1Index = 0, k2Index = 0;
542 uint8_t k0 = 0, k1 = 0, k2 = 0;
545 uint8_t msg3Delta = 0, msg3MinSchTime = 0;
547 uint8_t totalCfgSlot = 0;
549 uint16_t dciSlot = 0, rarSlot = 0;
550 SlotTimingInfo dciTime, rarTime, msg3Time, pucchTime;
551 RarAlloc *dciSlotAlloc = NULLP; /* Stores info for transmission of PDCCH for RAR */
552 RarAlloc *rarSlotAlloc = NULLP; /* Stores info for transmission of RAR PDSCH */
553 SchPuschInfo *msg3PuschInfo = NULLP; /* Stores MSG3 PUSCH scheduling information */
554 SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;
555 SchK2TimingInfoTbl *msg3K2InfoTbl=NULLP;
556 RaRspWindowStatus windowStatus=0;
559 totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbScs, cell->cellCfg.tddCfg.tddPeriod);
561 k0K1InfoTbl = &cell->k0K1InfoTbl;
562 if(cell->raReq[ueId-1]->isCFRA == false)
564 msg3K2InfoTbl = &cell->msg3K2InfoTbl;
565 puschMu = cell->numerology;
566 msg3Delta = puschDeltaTable[puschMu];
567 msg3MinSchTime = minMsg3SchTime[cell->numerology];
570 /* Calculating time frame to send DCI for RAR */
571 ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
572 dciSlot = dciTime.slot;
574 /* Consider this slot for sending DCI, only if it is a DL slot */
575 if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
578 /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
579 if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
582 /* Check if this slot is within RA response window */
583 windowStatus = isInRaRspWindow(cell->raReq[ueId-1], dciTime, cell->numSlots);
584 if(windowStatus == WITHIN_WINDOW)
586 /* For all k0 values, search for a suitable k2 value to schedule MSG3.
587 * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
588 * is found. Else no scheduling happens.
590 for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
592 k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
593 k0 = cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
595 /* Calculating time frame to send RAR PDSCH */
596 ADD_DELTA_TO_TIME(dciTime, rarTime, k0, cell->numSlots);
597 rarSlot = rarTime.slot;
599 /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
600 if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
603 /* If Contention-FREE RA is in progress, allocate resources for
604 * PUCCH for next UL message */
605 if(cell->raReq[ueId-1]->isCFRA)
607 numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
608 for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
610 k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
611 if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck)
613 k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck->\
614 dlDataToUlAckList[k1Index];
618 k1 = defaultUlAckTbl[k1Index];
621 ADD_DELTA_TO_TIME(rarTime, pucchTime, k1, cell->numSlots);
623 if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
626 if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
634 /* Else if contention-based RA is in progress, allocate resources for MSG3 */
635 for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
637 k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
638 k2 = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
640 /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
642 if(k2 >= msg3MinSchTime)
644 ADD_DELTA_TO_TIME(rarTime, msg3Time, k2, cell->numSlots);
646 if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
649 /* If PUSCH is already scheduled on this slot, another PUSCH
650 * pdu cannot be scheduled here */
651 if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
659 if(k1Found || k2Found)
663 else if(windowStatus == WINDOW_EXPIRED)
665 SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
666 SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
670 /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
671 if(!k1Found && !k2Found)
674 /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
675 SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
676 if(dciSlotAlloc == NULLP)
678 DU_LOG("\nERROR --> SCH : Memory Allocation failed for dciSlotAlloc");
681 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
683 /* Fill PDCCH and PDSCH scheduling information for RAR */
684 if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
686 DU_LOG("\nERROR --> SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
687 if(!dciSlotAlloc->rarPdschCfg)
689 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
690 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
696 dciSlotAlloc->raRnti = cell->raReq[ueId-1]->raRnti;
697 dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueId-1]->rachInd->crnti;
698 dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueId-1]->rachInd->preambleIdx;
699 dciSlotAlloc->rarInfo.ta = cell->raReq[ueId-1]->rachInd->timingAdv;
701 if(cell->raReq[ueId-1]->isCFRA)
703 /* Allocate resources for PUCCH */
704 schAllocPucchResource(cell, pucchTime, cell->raReq[ueId-1]->rachInd->crnti,NULLP, FALSE, NULLP);
708 /* Allocate resources for msg3 */
709 msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time, &(cell->raCb[ueId-1].msg3HqProc), FALSE);
712 dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.freqAlloc.numPrb;
713 /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
714 dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
715 dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.startPrb;
716 dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.numPrb;
717 dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
718 dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
719 dciSlotAlloc->rarInfo.ulGrant.tpc = 3; /* TODO : Check appropriate value to be filled */
720 /* Spec 38.213, section 8.2 : In a contention based random access
721 * procedure, the CSI request field is reserved. */
722 dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
726 /* Check if both DCI and RAR are sent in the same slot.
727 * If not, allocate memory RAR PDSCH slot to store RAR info
729 if(dciSlot == rarSlot)
731 SCH_ALLOC(dciSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
732 if(dciSlotAlloc->rarPdschCfg)
734 memcpy(dciSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci.pdschCfg, sizeof(PdschCfg));
738 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
739 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
740 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
741 DU_LOG("\nERROR --> SCH : Memory Allocation failed for dciSlotAlloc->rarPdschCfg");
748 /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
749 SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
750 if(rarSlotAlloc == NULLP)
752 DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarSlotAlloc");
753 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
754 if(!dciSlotAlloc->rarPdschCfg)
756 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
757 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
761 cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
763 /* Copy all RAR info */
764 rarSlotAlloc->raRnti = dciSlotAlloc->raRnti;
765 rarSlotAlloc->bwp = dciSlotAlloc->bwp;
766 SCH_ALLOC(rarSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
767 if(rarSlotAlloc->rarPdschCfg)
769 memcpy(rarSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci.pdschCfg,sizeof(PdschCfg));
773 DU_LOG("\nERROR --> SCH : Memory Allocation failed for rarSlotAlloc->rarPdschCfg");
774 SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
775 if(!dciSlotAlloc->rarPdschCfg)
777 SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
778 cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
780 SCH_FREE(rarSlotAlloc, sizeof(RarAlloc));
781 cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = NULLP;
786 cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
787 cell->schDlSlotInfo[rarSlot]->pdschUe = ueId;
788 if(cell->raReq[ueId-1]->isCFRA)
789 cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
791 cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
793 /* Create raCb at SCH */
794 createSchRaCb(ueId, cell->raReq[ueId-1], schInst);
796 /* Remove RachInd from pending RA request list */
797 SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
798 SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
806 * @brief process rach indication function.
810 * Function : SchProcRachInd
812 * This function process rach indication
814 * @param[in] rachInd parameters
815 * @param[in] shed instance
818 uint8_t SchProcRachInd(Pst *pst, RachIndInfo *rachInd)
820 SchRaReq *raReq = NULLP;
824 Inst schInst = pst->dstInst-SCH_INST_START;
825 SchCellCb *cell = schCb[schInst].cells[schInst];
827 DU_LOG("\nINFO --> SCH : Received Rach indication");
831 DU_LOG("\nERROR --> SCH: Failed to find cell in SchProcRachInd");
835 /* Storing RA request in cellCb */
836 GET_UE_ID(rachInd->crnti, ueId);
839 DU_LOG("\nERROR --> SCH: Invalid CRNTI [%d]", rachInd->crnti);
843 SCH_ALLOC(raReq, sizeof(SchRaReq));
846 DU_LOG("\nERROR --> SCH : Memory allocation failure in SchProcRachInd");
847 SCH_FREE(rachInd, sizeof(RachIndInfo));
851 /* calculate the ra-rnti value */
852 raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
853 raReq->rachInd = rachInd;
854 if((cell->ueCb[ueId-1].crnti == rachInd->crnti) && (cell->ueCb[ueId-1].state == SCH_UE_HANDIN_IN_PROGRESS))
856 raReq->isCFRA = true;
857 raReq->ueCb = &cell->ueCb[ueId-1];
860 raReq->isCFRA = false;
861 raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
862 raReq->winStartTime.slot = rachInd->timingInfo.slot;
864 /* Converting window size from ms to number of slots */
865 slotDuration = (1 / pow(2, cell->numerology));
866 winNumSlots = (float)cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.raRspWindow / slotDuration;
868 /* Adding window size to window start time to get window end time */
869 ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots, cell->numSlots);
870 cell->raReq[ueId -1] = raReq;
872 /* Adding UE Id to list of pending UEs to be scheduled */
873 cell->api->SchRachInd(cell, ueId);
878 * @brief fill RAR info function.
882 * Function : calculateRaRnti
884 * This function fills pdcch and pdsch info for RAR
886 * @param[in] rar Allocation info
889 * @param[in] offset to pointA to determine freq alloc
892 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
894 uint8_t coreset0Idx = 0;
895 uint8_t firstSymbol = 0, numSymbols = 0;
896 uint8_t mcs = DEFAULT_MCS; /* MCS fixed to 4 */
897 uint8_t dmrsStartSymbol, startSymbol, numSymbol ;
902 SchBwpDlCfg *initialBwp = &cell->cellCfg.dlCfgCommon.schInitialDlBwp;
903 SCH_ALLOC(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
904 if(rarAlloc->rarPdcchCfg == NULLP)
906 DU_LOG("\nERROR --> SCH : Memory allocation failed in %s",__func__);
909 PdcchCfg *pdcch = rarAlloc->rarPdcchCfg;
910 BwpCfg *bwp = &rarAlloc->bwp;
912 /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
913 coreset0Idx = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
914 numRbs = coresetIdxTable[coreset0Idx][1];
915 numSymbols = coresetIdxTable[coreset0Idx][2];
917 /* calculate time domain parameters */
918 // note: since slot value is made sl1, RAR can be sent at all slots
919 uint16_t mask = 0x2000;
920 for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
922 if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
929 bwp->freqAlloc.numPrb = initialBwp->bwp.freqAlloc.numPrb;
930 bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
931 bwp->subcarrierSpacing = initialBwp->bwp.scs;
932 bwp->cyclicPrefix = initialBwp->bwp.cyclicPrefix;
934 /* fill the PDCCH PDU */
935 pdcch->coresetCfg.startSymbolIndex = firstSymbol;
936 pdcch->coresetCfg.durationSymbols = numSymbols;
937 memcpy(pdcch->coresetCfg.freqDomainResource, \
938 cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
940 pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
941 pdcch->coresetCfg.regBundleSize = 6; /* spec-38.211 sec 7.3.2.2 */
942 pdcch->coresetCfg.interleaverSize = 2; /* spec-38.211 sec 7.3.2.2 */
943 pdcch->coresetCfg.coreSetType = 0;
944 pdcch->coresetCfg.coreSetSize = numRbs;
945 pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
946 pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
948 pdcch->dci.rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
949 pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
950 pdcch->dci.scramblingRnti = 0;
951 pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
952 pdcch->dci.aggregLevel = 4;
953 pdcch->dci.beamPdcchInfo.numPrgs = 1;
954 pdcch->dci.beamPdcchInfo.prgSize = 1;
955 pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
956 pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
957 pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
958 pdcch->dci.txPdcchPower.beta_pdcch_1_0 = 0;
959 pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
961 pdsch = &pdcch->dci.pdschCfg;
962 /* fill the PDSCH PDU */
964 pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
965 pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
967 pdsch->numCodewords = 1;
968 for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
970 pdsch->codeword[cwCount].targetCodeRate = 308;
971 pdsch->codeword[cwCount].qamModOrder = 2;
972 pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
973 pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
974 pdsch->codeword[cwCount].rvIndex = 0;
975 /* RAR PDU length and FAPI payload header length */
976 tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
977 pdsch->codeword[cwCount].tbSize = tbSize;
979 pdsch->dataScramblingId = cell->cellCfg.phyCellId;
980 pdsch->numLayers = 1;
981 pdsch->transmissionScheme = 0;
983 pdsch->dmrs.dlDmrsSymbPos = DL_DMRS_SYMBOL_POS;
984 pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
985 pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
986 pdsch->dmrs.scid = 0;
987 pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
988 pdsch->dmrs.dmrsPorts = 0;
989 pdsch->dmrs.mappingType = DMRS_MAP_TYPE_A; /* Type-A */
990 pdsch->dmrs.nrOfDmrsSymbols = NUM_DMRS_SYMBOLS;
991 pdsch->dmrs.dmrsAddPos = DMRS_ADDITIONAL_POS;
993 pdsch->pdschTimeAlloc.rowIndex = k0Index;
994 pdsch->pdschTimeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
995 pdsch->pdschTimeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
997 pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
998 pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
999 pdsch->pdschFreqAlloc.startPrb = MAX_NUM_RB;
1000 pdsch->pdschFreqAlloc.numPrb = \
1001 schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
1003 /* Find total symbols occupied including DMRS */
1004 dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
1005 /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
1006 * in that case only PDSCH symbols are marked as occupied */
1007 if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
1009 startSymbol = pdsch->pdschTimeAlloc.startSymb;
1010 numSymbol = pdsch->pdschTimeAlloc.numSymb;
1012 /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
1015 startSymbol = dmrsStartSymbol;
1016 numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
1019 /* Allocate the number of PRBs required for RAR PDSCH */
1020 if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
1021 &pdsch->pdschFreqAlloc.startPrb, pdsch->pdschFreqAlloc.numPrb)) != ROK)
1023 DU_LOG("\nERROR --> SCH : allocatePrbDl() failed for RAR");
1024 SCH_FREE(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
1028 pdsch->beamPdschInfo.numPrgs = 1;
1029 pdsch->beamPdschInfo.prgSize = 1;
1030 pdsch->beamPdschInfo.digBfInterfaces = 0;
1031 pdsch->beamPdschInfo.prg[0].pmIdx = 0;
1032 pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
1033 pdsch->txPdschPower.powerControlOffset = 0;
1034 pdsch->txPdschPower.powerControlOffsetSS = 0;
1039 /* @brief Process RACH resource release after CFRA
1043 * Function : SchProcRachRsrcRel
1045 * This function processes RACH resorce release
1046 * from MAC after CFRA. It releases the dedicated
1047 * preamble alloted to the UE
1049 * @param[in] Post structure
1050 * @param[in] RACH resource release
1054 uint8_t SchProcRachRsrcRel(Pst *pst, SchRachRsrcRel *schRachRsrcRel)
1057 uint8_t ssbIdx = 0, cfraSsbIdx = 0;
1058 uint16_t cellIdx = 0;
1059 Inst inst = pst->dstInst - SCH_INST_START;
1060 SchCellCb *cellCb = NULLP;
1061 SchUeCb *ueCb = NULLP;
1063 DU_LOG("\nINFO --> SCH : Received RACH resource release for Cell ID [%d] CRNTI [%d]", \
1064 schRachRsrcRel->cellId, schRachRsrcRel->crnti);
1067 for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1069 if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcRel->cellId))
1071 cellCb = schCb[inst].cells[cellIdx];
1079 ueCb = schGetUeCb(cellCb, schRachRsrcRel->crnti);
1080 if(ueCb->crnti != schRachRsrcRel->crnti)
1082 DU_LOG("\nERROR --> SCH : CRNTI [%d] not found", schRachRsrcRel->crnti);
1088 DU_LOG("\nERROR --> SCH : Cell ID [%d] not found", schRachRsrcRel->cellId);
1092 /* Free SSB resource if no failure has occurred until this step */
1095 for(ssbIdx = 0; ssbIdx < schRachRsrcRel->cfraResource.numSsb; ssbIdx++)
1097 /* Search each ssbIdx entry in UE Cb */
1098 for(cfraSsbIdx = 0; cfraSsbIdx < ueCb->cfraResource.numSsb; cfraSsbIdx++)
1100 if(ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx == schRachRsrcRel->cfraResource.ssbResource[ssbIdx].ssbIdx)
1102 /* If ssbIdx entry is found in UE CB, free dedicated resources
1103 * for this ssbIdx */
1104 UNSET_ONE_BIT(ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx, cellCb->dedPreambleBitMap);
1105 memset(&ueCb->cfraResource.ssbResource[cfraSsbIdx], 0, sizeof(SchCfraSsbResource));
1106 ueCb->cfraResource.numSsb--;
1113 /* Free RACH resource release memory allocated by MAC */
1114 SCH_FREE(schRachRsrcRel, sizeof(SchRachRsrcRel));
1117 /* @brief process MSG4 completion
1121 * Function : schMsg4Complete
1123 * This function updates ra state and msg4 Hqrq
1124 * proc upon MSG4 completion
1125 * @param[in] SchUeCb *ueCb, UE cb pointer
1128 void schMsg4Complete(SchUeCb *ueCb)
1130 DU_LOG("\nINFO --> SCH: State change for ueId[%2d] to SCH_RA_STATE_MSG4_DONE\n",ueCb->ueId);
1131 ueCb->cellCb->raCb[ueCb->ueId-1].raState = SCH_RA_STATE_MSG4_DONE;
1132 ueCb->msg4HqProc = ueCb->retxMsg4HqProc = NULLP;
1134 /**********************************************************************
1136 **********************************************************************/