[EPIC-ID: ODUHIGH-488][TASK-ID: ODUHIGH-494]SCH framework update to support different...
[o-du/l2.git] / src / 5gnrsch / sch_rach.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 *******************************************************************************/
18
19 /************************************************************************
20  
21      Name:     sch_rach.c
22   
23      Type:     C source file
24   
25      Desc:     C source code for rach handling functions
26   
27      File:     sch_rach.c
28   
29 **********************************************************************/
30
31 /** @file sch_rach.c
32 @brief This file implements the rach handling.
33 */
34 #include "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37 #include "tfu.x"
38 #include "lrg.x"
39 #include "du_log.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
42 #include "sch.h"
43 #include "sch_utils.h"
44
45 /**
46  * @brief Checks if PRACH can be scheduled in current slot
47  *
48  * @details
49  *
50  *     Function : schCheckPrachOcc
51  *
52  *     This function checks if PRACH can be scheduled in 
53  *     current slot
54  *
55  *  @param[in]  Cell Cb
56  *              Slot timing
57  *  @return  TRUE
58  *           FALSE
59  **/
60 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
61 {
62    uint8_t  prachCfgIdx = 0;
63    uint8_t  x = 0;
64    uint8_t  y = 0;
65    uint8_t  subFrame = 0;
66    uint16_t prachSubframe = 0;
67
68    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
69
70    /* derive the prachCfgIdx table paramters */
71    x                = prachCfgIdxTable[prachCfgIdx][1];
72    y                = prachCfgIdxTable[prachCfgIdx][2];
73    prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
74
75    if((prachOccasionTimingInfo.sfn%x) == y)
76    {
77       subFrame = prachOccasionTimingInfo.slot/pow(2, cell->cellCfg.numerology);
78
79       /* check for subFrame number */
80       if ((1 << subFrame) & prachSubframe)
81       {
82          /* prach ocassion present in this subframe */
83 #ifdef NR_TDD
84          if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
85          cell->slotFrmtBitMap))
86          {
87             DU_LOG("\nERROR  --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
88             return FALSE;
89          }
90 #endif
91          return TRUE;
92       }
93    }
94    return FALSE;
95 }
96
97 /**
98  * @brief Calculate number of PRBs to be allocated for PRACH 
99  *
100  * @details
101  *
102  *     Function : schCalcPrachNumRb
103  *
104  *     Calculate number of PRBs to be allocated for PRACH
105  *
106  *  @param[in]  SchCellCb *cell, cell cb
107  *  @return  Number of PRBs
108  **/
109 uint8_t schCalcPrachNumRb(SchCellCb *cell)
110 {
111    uint8_t tableIdx = 0;
112    uint16_t puschScs = convertScsEnumValToScsVal(cell->cellCfg.schInitialUlBwp.bwp.scs);
113
114    for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
115    {
116       if((numRbForPrachTable[tableIdx][0] == cell->cellCfg.schRachCfg.rootSeqLen) &&
117             (numRbForPrachTable[tableIdx][1] == cell->cellCfg.schRachCfg.prachSubcSpacing) &&
118             (numRbForPrachTable[tableIdx][2] == puschScs))
119       {
120          return numRbForPrachTable[tableIdx][3];
121       }
122    }
123    return 0;
124 }
125
126 /**
127  * @brief resource allocation for PRACH
128  *
129  * @details
130  *
131  *     Function : schPrachResAlloc
132  *
133  *     This function handles PRACH allocation
134  *
135  *  @param[in]  SchCellCb *cell, cell cb
136  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
137  *  @return  void
138  **/
139 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
140 {
141    uint8_t  numPrachRb = 0;
142    uint8_t  numRa = 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;
150
151    if(cell == NULLP)
152    {
153       DU_LOG("\nERROR  --> SCH : schPrachResAlloc(): Received cellCb is null");
154       return;
155    }
156
157    /* If this slot is not a PRACH occassion, return */
158    if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
159       return;
160
161    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
162    prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
163    prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
164    prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
165    prachDuration    = prachCfgIdxTable[prachCfgIdx][7];
166
167    /* numRa determined as 𝑛 belonging {0,1,.., M − 1},
168     * where M is given by msg1Fdm */
169    numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
170
171    /* freq domain resource determination for RACH*/
172    freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
173    numPrachRb = schCalcPrachNumRb(cell);
174    /* Allocate PRACH resources from the UL resource bitmap */
175    allocatePrbUl(cell, prachOccasionTimingInfo, prachStartSymbol, prachDuration, &freqStart, numPrachRb);
176
177    /* prach info */
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);
185 }
186
187 /**
188  * @brief Process RACH resource request for CFRA
189  *
190  * @details
191  *
192  *     Function : SchProcRachRsrcReq
193  *     
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
197  *     response
198  *     
199  *  @param[in]  Post structure
200  *  @param[in]  RACH resource request
201  *  @return     ROK
202  *              RFAILED
203  **/
204 uint8_t SchProcRachRsrcReq(Pst *pst, SchRachRsrcReq *schRachRsrcReq)
205 {
206    uint8_t      ssbIdx = 0, cfraSsbIdx = 0;
207    uint8_t      firstCFPreambleIndex = 0, lastCFPreambleIndex = 0;
208    uint16_t     cellIdx = 0;
209    uint64_t     mask = 0;
210    Pst          rspPst;
211    Inst         inst = pst->dstInst - SCH_INST_START;
212    SchCellCb    *cellCb = NULLP;
213    SchUeCb      *ueCb = NULLP;
214    SchRachRsrcRsp *rachRsrcRsp = NULLP;
215
216    DU_LOG("\nINFO  -->  SCH : Received RACH resource request for Cell ID [%d] CRNTI [%d]", \
217          schRachRsrcReq->cellId, schRachRsrcReq->crnti);
218
219  /* Fill RACH resource response to MAC */
220    SCH_ALLOC(rachRsrcRsp, sizeof(SchRachRsrcRsp));
221    if(!rachRsrcRsp)
222    {   
223       DU_LOG("\nERROR  -->  SCH : Memory allocation failed for RACH resource response");
224       return RFAILED;
225    }   
226    rachRsrcRsp->cellId = schRachRsrcReq->cellId;
227    rachRsrcRsp->crnti = schRachRsrcReq->crnti;
228    rachRsrcRsp->result = RSP_OK;
229
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;
234
235    /* Fetch Cell CB */
236    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
237    {
238       if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcReq->cellId))
239       {
240          cellCb = schCb[inst].cells[cellIdx];
241          break;
242       }
243    }
244    
245    if(cellCb)
246    {
247       /* Fetch UE CB */
248       ueCb = schGetUeCb(cellCb, schRachRsrcReq->crnti);
249       if(ueCb->crnti != schRachRsrcReq->crnti)
250       {
251          DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found" ,schRachRsrcReq->crnti);
252          rachRsrcRsp->result = RSP_NOK;
253       }
254    }
255    else
256    {
257       DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found" ,schRachRsrcReq->cellId);
258       rachRsrcRsp->result = RSP_NOK;
259    }
260
261    /* Allocate SSB resource if no failure has occurred until this step */
262    if(rachRsrcRsp->result == RSP_OK)
263    {
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.schRachCfg.numCbPreamblePerSsb;
269       lastCFPreambleIndex = cellCb->cellCfg.schRachCfg.totalNumRaPreamble;
270
271       /* Allocate resource for each SSB index requested */
272       for(ssbIdx = 0; ssbIdx < schRachRsrcReq->numSsb; ssbIdx++)
273       {
274          /* Find the first CF Preamble index not dedicated to any UE currently */
275          while(firstCFPreambleIndex <= lastCFPreambleIndex)
276          {
277             mask = 1 << firstCFPreambleIndex;
278             if(cellCb->dedPreambleBitMap & mask)
279             {
280                firstCFPreambleIndex++;
281                continue;
282             }
283             else
284                break;
285          }
286
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)
294          {
295             ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx = schRachRsrcReq->ssbIdx[ssbIdx]; 
296             ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx = firstCFPreambleIndex;
297             SET_ONE_BIT(firstCFPreambleIndex, cellCb->dedPreambleBitMap);
298             cfraSsbIdx++;
299             firstCFPreambleIndex++;
300          }
301          else
302          {
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 */
306             break;
307          }
308       } /* End of for */
309
310       ueCb->cfraResource.numSsb = cfraSsbIdx;
311
312       if(ueCb->cfraResource.numSsb == 0)
313       {
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;
317       }
318       else
319       {   
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));
324       }
325    } /* End of if */
326
327    /* Free RACH resource request memory allocated by MAC */
328    SCH_FREE(schRachRsrcReq, sizeof(SchRachRsrcReq));
329
330    /* Send RACH resource response to MAC */
331    return(MacMessageRouter(&rspPst, (void *)rachRsrcRsp));
332 }
333
334 /**
335  * @brief calculate ra-rnti function. 
336  *
337  * @details
338  *
339  *     Function : calculateRaRnti
340  *     
341  *     This function calculates ra-rnti
342  *     
343  *  @param[in]  symbol index
344  *  @param[in]  slot index
345  *  @param[in]  frequency index
346  *  @return  ra-rnti
347  **/
348 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
349 {
350    uint16_t raRnti = 0;
351    uint8_t  ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
352    
353    /* Refer to spec 38.321, section 5.1.3 */
354    raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
355    return raRnti;
356 }
357
358 /**
359  * @brief create raCb function. 
360  *
361  * @details
362  *
363  *     Function : createSchRaCb
364  *     
365  *     This function create raCb
366  *     
367  *  @param[in]  crnti
368  *  @param[in]  shed instance
369  *  @return  void
370  **/
371 void createSchRaCb(uint8_t ueId, SchRaReq *raReq, Inst schInst)
372 {
373    if(raReq->isCFRA)
374    {
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))
378       {
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;
383       }
384    }
385    else
386    {
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;
392    }
393    schCb[schInst].cells[schInst]->raCb[ueId -1].cell = schCb[schInst].cells[schInst];
394 }
395
396 /**
397  * @brief resource allocation for msg3 PUSCH
398  *
399  * @details
400  *
401  *     Function : schAllocMsg3Pusch 
402  *     
403  *     This function handles msg3 PUSCH allocation
404  *     
405  *  @param[in]  Inst schInst, SCH instance
406  *  @param[in]  slot, current slot
407  *  @param[out]  msg3StartRb
408  *  @param[out]  msg3NumRb
409  *  @return  void
410  **/
411 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime, SchUlHqProcCb* msg3HqProc, bool isRetx)
412 {
413    SchCellCb      *cell          = NULLP;
414    SchUlSlotInfo  *schUlSlotInfo = NULLP;
415    uint8_t    mcs       = DEFAULT_MCS;
416    uint8_t    startSymb = 0;
417    uint8_t    symbLen   = 0; 
418    uint16_t   startRb   = 0;
419    uint16_t   numRb     = 0;
420    uint16_t   tbSize    = 0;
421
422    cell = schCb[schInst].cells[schInst];
423    if(cell == NULL)
424    {
425       DU_LOG("\n\nERROR  -->  SCH :  Failed to find cell in schAllocMsg3Pusch");
426       return NULLP;
427    }
428
429    /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
430    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
431    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
432
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);
438
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)
443    {
444       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
445       return NULLP;
446    }
447
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*/
451
452    schUlSlotInfo->schPuschInfo->crnti             = crnti;
453    schUlSlotInfo->schPuschInfo->harqProcId        = msg3HqProc->procId;
454    schUlSlotInfo->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
455    schUlSlotInfo->schPuschInfo->fdAlloc.startPrb  = startRb;
456    schUlSlotInfo->schPuschInfo->fdAlloc.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;
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;
468    if(!isRetx)
469    {
470       msg3HqProc->strtSymbl = startSymb;
471       msg3HqProc->numSymbl = symbLen;
472       msg3HqProc->puschResType = schUlSlotInfo->schPuschInfo->resAllocType;
473       msg3HqProc->puschStartPrb = schUlSlotInfo->schPuschInfo->fdAlloc.startPrb;
474       msg3HqProc->puschNumPrb = schUlSlotInfo->schPuschInfo->fdAlloc.numPrb;
475       msg3HqProc->tbInfo.qamOrder = schUlSlotInfo->schPuschInfo->tbInfo.qamOrder;
476       msg3HqProc->tbInfo.iMcs = schUlSlotInfo->schPuschInfo->tbInfo.mcs;
477       msg3HqProc->tbInfo.mcsTable = schUlSlotInfo->schPuschInfo->tbInfo.mcsTable;
478       msg3HqProc->tbInfo.ndi = schUlSlotInfo->schPuschInfo->tbInfo.ndi;
479       msg3HqProc->tbInfo.rv = schUlSlotInfo->schPuschInfo->tbInfo.rv;
480       msg3HqProc->tbInfo.tbSzReq = schUlSlotInfo->schPuschInfo->tbInfo.tbSize;
481       msg3HqProc->dmrsMappingType = schUlSlotInfo->schPuschInfo->dmrsMappingType;
482       msg3HqProc->nrOfDmrsSymbols = schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols;
483       msg3HqProc->dmrsAddPos = schUlSlotInfo->schPuschInfo->dmrsAddPos;
484    }
485    return schUlSlotInfo->schPuschInfo;
486 }
487
488 /**
489  * @brief Check if a time frame is in RA Response window
490  *
491  * @details
492  *
493  *     Function : isInRaRspWindow
494  *
495  *     Check if a time frame is in RA Response window
496  *
497  *  @param[in]  RA request
498  *  @param[in]  Time frame to check
499  *  @param[in]  Total number of slot per radio frame
500  *  @return  true 
501  *  @return  false
502  **/
503 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
504 {
505    uint32_t winStartTime, winEndTime, timeToCheck;
506    
507    winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
508    winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
509    timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
510
511    /* TODO : check how to handle the wrap around scenario of MAX_SFN */
512    if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
513       return WITHIN_WINDOW;
514    else if(timeToCheck < winStartTime)
515       return WINDOW_YET_TO_START;
516       
517    return WINDOW_EXPIRED;
518 }
519
520 /**
521  * @brief Processes any pending RA request
522  *
523  * @details
524  *
525  *     Function : schProcessRaReq
526  *
527  *     This function process pending RA request
528  *
529  *  @param[in]  Current timing of the cell
530  *  @return  ROK
531  **/
532 bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
533 {
534    bool      k1Found = false, k2Found = false;
535    uint8_t   k0TblIdx = 0, k1TblIdx = 0, k2TblIdx = 0;
536    uint8_t   k0Index = 0, k1Index = 0, k2Index = 0;
537    uint8_t   k0 = 0, k1 = 0, k2 = 0;
538    uint8_t   numK1 = 0;
539    uint8_t   puschMu = 0;
540    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
541 #ifdef NR_TDD
542    uint8_t   totalCfgSlot = 0;
543 #endif
544    uint16_t             dciSlot = 0, rarSlot = 0;
545    SlotTimingInfo       dciTime, rarTime, msg3Time, pucchTime;
546    RarAlloc             *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for RAR */
547    RarAlloc             *rarSlotAlloc = NULLP;    /* Stores info for transmission of RAR PDSCH */
548    SchPuschInfo         *msg3PuschInfo = NULLP;   /* Stores MSG3 PUSCH scheduling information */
549    SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;    
550    SchK2TimingInfoTbl   *msg3K2InfoTbl=NULLP;
551    RaRspWindowStatus    windowStatus=0;
552    
553 #ifdef NR_TDD
554    totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
555 #endif
556    k0K1InfoTbl    = &cell->cellCfg.schInitialDlBwp.k0K1InfoTbl;
557    if(cell->raReq[ueId-1]->isCFRA == false)
558    {
559       msg3K2InfoTbl  = &cell->cellCfg.schInitialUlBwp.msg3K2InfoTbl;
560       puschMu        = cell->cellCfg.numerology;
561       msg3Delta      = puschDeltaTable[puschMu];
562       msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
563    }
564
565    /* Calculating time frame to send DCI for RAR */
566    ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
567    dciSlot = dciTime.slot;
568 #ifdef NR_TDD
569    /* Consider this slot for sending DCI, only if it is a DL slot */
570    if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
571 #endif
572    {
573       /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
574       if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
575          return false;
576
577       /* Check if this slot is within RA response window */
578       windowStatus = isInRaRspWindow(cell->raReq[ueId-1], dciTime, cell->numSlots);
579       if(windowStatus == WITHIN_WINDOW)
580       {
581          /* For all k0 values, search for a suitable k2 value to schedule MSG3.
582           * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
583           * is found. Else no scheduling happens. 
584           */
585          for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
586          {
587             k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
588             k0 = cell->cellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
589
590             /* Calculating time frame to send RAR PDSCH */
591             ADD_DELTA_TO_TIME(dciTime, rarTime, k0, cell->numSlots);
592             rarSlot = rarTime.slot;
593             
594             /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
595             if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
596                continue;
597
598             /* If Contention-FREE RA is in progress, allocate resources for
599              * PUCCH for next UL message */
600             if(cell->raReq[ueId-1]->isCFRA)
601             {
602                numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
603                for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
604                {   
605                   k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
606                   if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck)
607                   {
608                      k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck->\
609                         dlDataToUlAckList[k1Index];
610                   }
611                   else
612                   {
613                      k1 = defaultUlAckTbl[k1Index];
614                   }
615
616                   ADD_DELTA_TO_TIME(rarTime, pucchTime, k1, cell->numSlots);
617 #ifdef NR_TDD
618                   if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
619                      continue;
620 #endif
621                   if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
622                      continue;
623                   k1Found = true;
624                   break;
625                }
626             }
627             else
628             {
629                /* Else if contention-based RA is in progress, allocate resources for MSG3 */
630                for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
631                {
632                   k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
633                   k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
634
635                   /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
636                   k2 = k2 + msg3Delta;
637                   if(k2 >= msg3MinSchTime)
638                   {
639                      ADD_DELTA_TO_TIME(rarTime, msg3Time, k2, cell->numSlots);
640 #ifdef NR_TDD
641                      if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
642                         continue;
643 #endif
644                      /* If PUSCH is already scheduled on this slot, another PUSCH
645                       * pdu cannot be scheduled here */
646                      if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
647                         continue;
648
649                      k2Found = true;
650                      break;
651                   }
652                }
653             }
654             if(k1Found || k2Found)
655                break;
656          }
657       }
658       else if(windowStatus == WINDOW_EXPIRED)
659       {
660          SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
661          SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
662          return false;
663       }
664
665       /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
666       if(!k1Found && !k2Found)
667          return false;
668
669       /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
670       SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
671       if(dciSlotAlloc == NULLP)
672       {
673          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
674          return false;
675       }
676       cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
677
678       /* Fill PDCCH and PDSCH scheduling information for RAR */
679       if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
680       {
681          DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
682          SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
683          cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
684          return false;
685       }
686
687       /* Fill RAR info */
688       dciSlotAlloc->rarInfo.raRnti = cell->raReq[ueId-1]->raRnti;
689       dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueId-1]->rachInd->crnti;
690       dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueId-1]->rachInd->preambleIdx;
691       dciSlotAlloc->rarInfo.ta = cell->raReq[ueId-1]->rachInd->timingAdv;
692
693       if(cell->raReq[ueId-1]->isCFRA)
694       {
695          /* Allocate resources for PUCCH */
696          schAllocPucchResource(cell, pucchTime, cell->raReq[ueId-1]->rachInd->crnti,NULLP, FALSE, NULLP);
697       }
698       else
699       {
700          /* Allocate resources for msg3 */
701          msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time, &(cell->raCb[ueId-1].msg3HqProc), FALSE);
702          if(msg3PuschInfo)
703          {
704             dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb;
705             /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
706             dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
707             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.startPrb;
708             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.numPrb;
709             dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
710             dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
711             dciSlotAlloc->rarInfo.ulGrant.tpc = 3;  /* TODO : Check appropriate value to be filled */
712             /* Spec 38.213, section 8.2 : In a contention based random access
713              * procedure, the CSI request field is reserved. */
714             dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
715          }
716       }
717
718       /* Check if both DCI and RAR are sent in the same slot.
719        * If not, allocate memory RAR PDSCH slot to store RAR info
720        */
721       if(dciSlot == rarSlot)
722          dciSlotAlloc->pduPres = BOTH;
723       else
724       {
725          /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
726          SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
727          if(rarSlotAlloc == NULLP)
728          {
729             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
730             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
731             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
732             return false;
733          }
734          cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
735
736          /* Copy all RAR info */
737          memcpy(rarSlotAlloc, dciSlotAlloc, sizeof(RarAlloc));
738          rarSlotAlloc->rarPdcchCfg.dci.pdschCfg = &rarSlotAlloc->rarPdschCfg;
739
740          /* Assign correct PDU types in corresponding slots */
741          rarSlotAlloc->pduPres = PDSCH_PDU;
742          dciSlotAlloc->pduPres = PDCCH_PDU;
743          dciSlotAlloc->pdschSlot = rarSlot;  
744       }
745
746       cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
747       cell->schDlSlotInfo[rarSlot]->pdschUe = ueId;
748       if(cell->raReq[ueId-1]->isCFRA)
749          cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
750       else
751          cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
752
753       /* Create raCb at SCH */
754       createSchRaCb(ueId, cell->raReq[ueId-1], schInst);
755
756       /* Remove RachInd from pending RA request list */
757       SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
758       SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
759       
760       return true;
761    }
762    return false;
763 }
764
765 /**
766  * @brief process rach indication function. 
767  *
768  * @details
769  *
770  *     Function : SchProcRachInd
771  *     
772  *     This function process rach indication
773  *     
774  *  @param[in]  rachInd parameters
775  *  @param[in]  shed instance
776  *  @return  ROK
777  **/
778 uint8_t SchProcRachInd(Pst *pst, RachIndInfo *rachInd)
779 {
780    SchRaReq  *raReq = NULLP;
781    float     slotDuration;
782    uint8_t   winNumSlots;
783    uint8_t   ueId;
784    Inst      schInst = pst->dstInst-SCH_INST_START;
785    SchCellCb *cell = schCb[schInst].cells[schInst];
786
787    DU_LOG("\nINFO  -->  SCH : Received Rach indication");
788
789    if(cell == NULLP)
790    {
791       DU_LOG("\nERROR  -->  SCH: Failed to find cell in SchProcRachInd");
792       return RFAILED;
793    }
794
795    /* Storing RA request in cellCb */
796    GET_UE_ID(rachInd->crnti, ueId);
797    if(ueId <= 0)
798    {
799       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
800       return RFAILED;
801    }
802
803    SCH_ALLOC(raReq, sizeof(SchRaReq));
804    if(!raReq)
805    {
806       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in SchProcRachInd");
807       SCH_FREE(rachInd, sizeof(RachIndInfo));
808       return RFAILED;
809    }
810
811    /* calculate the ra-rnti value */
812    raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
813    raReq->rachInd = rachInd;
814    if((cell->ueCb[ueId-1].crnti == rachInd->crnti) && (cell->ueCb[ueId-1].state == SCH_UE_HANDIN_IN_PROGRESS))
815    {
816       raReq->isCFRA = true;
817       raReq->ueCb = &cell->ueCb[ueId-1];
818    }
819    else
820       raReq->isCFRA = false;
821    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
822    raReq->winStartTime.slot = rachInd->timingInfo.slot;
823   
824    /* Converting window size from ms to number of slots */
825    slotDuration = (1 / pow(2, cell->cellCfg.numerology));
826    winNumSlots = (float)cell->cellCfg.schRachCfg.raRspWindow / slotDuration;
827    
828    /* Adding window size to window start time to get window end time */
829    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots, cell->numSlots);
830    cell->raReq[ueId -1] = raReq;
831
832    /* Adding UE Id to list of pending UEs to be scheduled */
833    cell->api->SchRachInd(cell, ueId);
834    return ROK;
835 }
836
837 /**
838  * @brief fill RAR info function. 
839  *
840  * @details
841  *
842  *     Function : calculateRaRnti
843  *     
844  *     This function fills pdcch and pdsch info for RAR
845  *     
846  *  @param[in]  rar Allocation info
847  *  @param[in]  ra-rnti
848  *  @param[in]  PCI
849  *  @param[in]  offset to pointA to determine freq alloc
850  *  @return  ROK
851  **/
852 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
853 {
854    uint8_t  coreset0Idx = 0;
855    uint8_t  firstSymbol = 0, numSymbols = 0;
856    uint8_t  mcs = DEFAULT_MCS;  /* MCS fixed to 4 */
857    uint8_t  dmrsStartSymbol, startSymbol, numSymbol ;
858    uint16_t numRbs = 0;
859    uint16_t tbSize = 0;
860
861    SchBwpDlCfg *initialBwp = &cell->cellCfg.schInitialDlBwp;
862    PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
863    PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
864    BwpCfg *bwp = &rarAlloc->bwp;
865
866    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
867    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
868    numRbs     = coresetIdxTable[coreset0Idx][1]; 
869    numSymbols = coresetIdxTable[coreset0Idx][2];
870
871    /* calculate time domain parameters */
872    // note: since slot value is made sl1, RAR can be sent at all slots
873    uint16_t mask = 0x2000;
874    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
875    {
876       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
877          break;
878       else
879          mask = mask>>1;
880    }
881
882    /* fill BWP */
883    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
884    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
885    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
886    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
887
888    /* fill the PDCCH PDU */
889    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
890    pdcch->coresetCfg.durationSymbols = numSymbols;
891    memcpy(pdcch->coresetCfg.freqDomainResource, \
892       cell->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
893
894    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
895    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
896    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
897    pdcch->coresetCfg.coreSetType = 0;
898    pdcch->coresetCfg.coreSetSize = numRbs;
899    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
900    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
901    pdcch->numDlDci = 1;
902    pdcch->dci.rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
903    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
904    pdcch->dci.scramblingRnti = 0;
905    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
906    pdcch->dci.aggregLevel = 4;
907    pdcch->dci.beamPdcchInfo.numPrgs = 1;
908    pdcch->dci.beamPdcchInfo.prgSize = 1;
909    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
910    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
911    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
912    pdcch->dci.txPdcchPower.powerValue = 0;
913    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
914    pdcch->dci.pdschCfg = pdsch;
915
916    /* fill the PDSCH PDU */
917    uint8_t cwCount = 0;
918    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
919    pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
920    pdsch->pduIndex = 0;
921    pdsch->numCodewords = 1;
922    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
923    {
924       pdsch->codeword[cwCount].targetCodeRate = 308;
925       pdsch->codeword[cwCount].qamModOrder = 2;
926       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
927       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
928       pdsch->codeword[cwCount].rvIndex = 0;
929       /* RAR PDU length and FAPI payload header length */
930       tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
931       pdsch->codeword[cwCount].tbSize = tbSize;
932    }
933    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
934    pdsch->numLayers = 1;
935    pdsch->transmissionScheme = 0;
936    pdsch->refPoint = 0;
937    pdsch->dmrs.dlDmrsSymbPos = 4;  /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
938    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
939    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
940    pdsch->dmrs.scid = 0;
941    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
942    pdsch->dmrs.dmrsPorts = 0;
943    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
944    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
945    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
946
947    pdsch->pdschTimeAlloc.rowIndex = k0Index;
948    pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
949    pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
950
951    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
952    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
953    pdsch->pdschFreqAlloc.freqAlloc.startPrb = MAX_NUM_RB;
954    pdsch->pdschFreqAlloc.freqAlloc.numPrb = \
955       schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
956
957    /* Find total symbols occupied including DMRS */
958    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
959    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT, 
960     * in that case only PDSCH symbols are marked as occupied */
961    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
962    {
963       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
964       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
965    }
966    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
967    else
968    {
969       startSymbol = dmrsStartSymbol;
970       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
971    }
972
973    /* Allocate the number of PRBs required for RAR PDSCH */
974    if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
975       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
976    {
977       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
978       return RFAILED;
979    }
980
981    pdsch->beamPdschInfo.numPrgs = 1;
982    pdsch->beamPdschInfo.prgSize = 1;
983    pdsch->beamPdschInfo.digBfInterfaces = 0;
984    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
985    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
986    pdsch->txPdschPower.powerControlOffset = 0;
987    pdsch->txPdschPower.powerControlOffsetSS = 0;
988
989    return ROK;
990 }
991
992  /* @brief Process RACH resource release after CFRA
993  *
994  * @details
995  *
996  *     Function : SchProcRachRsrcRel
997  *     
998  *     This function processes RACH resorce release
999  *     from MAC after CFRA. It releases the dedicated 
1000  *     preamble alloted to the UE
1001  *     
1002  *  @param[in]  Post structure
1003  *  @param[in]  RACH resource release
1004  *  @return     ROK
1005  *              RFAILED
1006  */
1007 uint8_t SchProcRachRsrcRel(Pst *pst, SchRachRsrcRel *schRachRsrcRel)
1008 {
1009    uint8_t      ret = ROK;
1010    uint8_t      ssbIdx = 0, cfraSsbIdx = 0;
1011    uint16_t     cellIdx = 0;
1012    Inst         inst = pst->dstInst - SCH_INST_START;
1013    SchCellCb    *cellCb = NULLP;
1014    SchUeCb      *ueCb = NULLP;
1015
1016    DU_LOG("\nINFO  -->  SCH : Received RACH resource release for Cell ID [%d] CRNTI [%d]", \
1017          schRachRsrcRel->cellId, schRachRsrcRel->crnti);
1018
1019    /* Fetch Cell CB */
1020    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1021    {
1022       if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcRel->cellId))
1023       {
1024          cellCb = schCb[inst].cells[cellIdx];
1025          break;
1026       }
1027    }
1028    
1029    if(cellCb)
1030    {
1031       /* Fetch UE CB */
1032       ueCb = schGetUeCb(cellCb, schRachRsrcRel->crnti);
1033       if(ueCb->crnti != schRachRsrcRel->crnti)
1034       {
1035          DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found", schRachRsrcRel->crnti);
1036          ret = RFAILED;
1037       }
1038    }
1039    else
1040    {
1041       DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found", schRachRsrcRel->cellId);
1042       ret = RFAILED;
1043    }
1044
1045    /* Free SSB resource if no failure has occurred until this step */
1046    if(ret == ROK)
1047    {
1048       for(ssbIdx = 0; ssbIdx < schRachRsrcRel->cfraResource.numSsb; ssbIdx++)
1049       {
1050          /* Search each ssbIdx entry in UE Cb */
1051          for(cfraSsbIdx = 0; cfraSsbIdx < ueCb->cfraResource.numSsb; cfraSsbIdx++)
1052          {
1053             if(ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx == schRachRsrcRel->cfraResource.ssbResource[ssbIdx].ssbIdx)
1054             {
1055                /* If ssbIdx entry is found in UE CB, free dedicated resources
1056                 * for this ssbIdx */
1057                UNSET_ONE_BIT(ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx, cellCb->dedPreambleBitMap);
1058                memset(&ueCb->cfraResource.ssbResource[cfraSsbIdx], 0, sizeof(SchCfraSsbResource));
1059                ueCb->cfraResource.numSsb--;
1060                break;
1061             }
1062          }
1063       } /* End of for */
1064    } /* End of if */
1065
1066    /* Free RACH resource release memory allocated by MAC */
1067    SCH_FREE(schRachRsrcRel, sizeof(SchRachRsrcRel));
1068    return ret;
1069 }
1070  /* @brief process MSG4 completion
1071  *
1072  * @details
1073  *
1074  *     Function : schMsg4Complete
1075  *     
1076  *     This function updates ra state and msg4 Hqrq 
1077  *     proc upon MSG4 completion     
1078  *  @param[in]  SchUeCb *ueCb, UE cb pointer
1079  *  @return     VOID
1080  */
1081 void schMsg4Complete(SchUeCb *ueCb)
1082 {
1083    DU_LOG("\nINFO --> SCH: State change for ueId[%2d] to SCH_RA_STATE_MSG4_DONE\n",ueCb->ueId);
1084    ueCb->cellCb->raCb[ueCb->ueId-1].raState = SCH_RA_STATE_MSG4_DONE;
1085    ueCb->msg4HqProc = ueCb->retxMsg4HqProc = NULLP;
1086 }
1087 /**********************************************************************
1088          End of file
1089 **********************************************************************/