[Epic-ID: ODUHIGH-517][Task-ID: ODUHIGH-540][Sub-Task-ID: ODUHIGH-548]| MUlti UE...
[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_tmr.h"
44 #include "sch_utils.h"
45
46 /**
47  * @brief Checks if PRACH can be scheduled in current slot
48  *
49  * @details
50  *
51  *     Function : schCheckPrachOcc
52  *
53  *     This function checks if PRACH can be scheduled in 
54  *     current slot
55  *
56  *  @param[in]  Cell Cb
57  *              Slot timing
58  *  @return  TRUE
59  *           FALSE
60  **/
61 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
62 {
63    uint8_t  prachCfgIdx = 0;
64    uint8_t  x = 0;
65    uint8_t  y = 0;
66    uint8_t  subFrame = 0;
67    uint16_t prachSubframe = 0;
68
69    prachCfgIdx      = cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx;
70
71    /* derive the prachCfgIdx table paramters */
72    x                = prachCfgIdxTable[prachCfgIdx][1];
73    y                = prachCfgIdxTable[prachCfgIdx][2];
74    prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
75
76    if((prachOccasionTimingInfo.sfn%x) == y)
77    {
78       subFrame = prachOccasionTimingInfo.slot/pow(2, cell->numerology);
79
80       /* check for subFrame number */
81       if ((1 << subFrame) & prachSubframe)
82       {
83          /* prach ocassion present in this subframe */
84 #ifdef NR_TDD
85          if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
86                                              cell->slotFrmtBitMap))
87          {
88             DU_LOG("\nERROR  --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
89             return FALSE;
90          }
91 #endif
92          return TRUE;
93       }
94    }
95    return FALSE;
96 }
97
98 /**
99  * @brief Calculate number of PRBs to be allocated for PRACH 
100  *
101  * @details
102  *
103  *     Function : schCalcPrachNumRb
104  *
105  *     Calculate number of PRBs to be allocated for PRACH
106  *
107  *  @param[in]  SchCellCb *cell, cell cb
108  *  @return  Number of PRBs
109  **/
110 uint8_t schCalcPrachNumRb(SchCellCb *cell)
111 {
112    uint8_t tableIdx = 0;
113    uint16_t puschScs = convertScsEnumValToScsVal(cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.scs);
114
115    for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
116    {
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))
120       {
121          return numRbForPrachTable[tableIdx][3];
122       }
123    }
124    return 0;
125 }
126
127 /**
128  * @brief resource allocation for PRACH
129  *
130  * @details
131  *
132  *     Function : schPrachResAlloc
133  *
134  *     This function handles PRACH allocation
135  *
136  *  @param[in]  SchCellCb *cell, cell cb
137  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
138  *  @return  void
139  **/
140 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
141 {
142    uint8_t  numPrachRb = 0;
143    uint8_t  numRa = 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;
151
152    if(cell == NULLP)
153    {
154       DU_LOG("\nERROR  --> SCH : schPrachResAlloc(): Received cellCb is null");
155       return;
156    }
157
158    /* If this slot is not a PRACH occassion, return */
159    if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
160       return;
161
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];
167
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);
171
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);
177
178    /* prach info */
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);
186 }
187
188 /**
189  * @brief Process RACH resource request for CFRA
190  *
191  * @details
192  *
193  *     Function : SchProcRachRsrcReq
194  *     
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
198  *     response
199  *     
200  *  @param[in]  Post structure
201  *  @param[in]  RACH resource request
202  *  @return     ROK
203  *              RFAILED
204  **/
205 uint8_t SchProcRachRsrcReq(Pst *pst, SchRachRsrcReq *schRachRsrcReq)
206 {
207    uint8_t      ssbIdx = 0, cfraSsbIdx = 0;
208    uint8_t      firstCFPreambleIndex = 0, lastCFPreambleIndex = 0;
209    uint16_t     cellIdx = 0;
210    uint64_t     mask = 0;
211    Pst          rspPst;
212    Inst         inst = pst->dstInst - SCH_INST_START;
213    SchCellCb    *cellCb = NULLP;
214    SchUeCb      *ueCb = NULLP;
215    SchRachRsrcRsp *rachRsrcRsp = NULLP;
216
217    DU_LOG("\nINFO  -->  SCH : Received RACH resource request for Cell ID [%d] CRNTI [%d]", \
218          schRachRsrcReq->cellId, schRachRsrcReq->crnti);
219
220  /* Fill RACH resource response to MAC */
221    SCH_ALLOC(rachRsrcRsp, sizeof(SchRachRsrcRsp));
222    if(!rachRsrcRsp)
223    {   
224       DU_LOG("\nERROR  -->  SCH : Memory allocation failed for RACH resource response");
225       return RFAILED;
226    }   
227    rachRsrcRsp->cellId = schRachRsrcReq->cellId;
228    rachRsrcRsp->crnti = schRachRsrcReq->crnti;
229    rachRsrcRsp->result = RSP_OK;
230
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;
235
236    /* Fetch Cell CB */
237    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
238    {
239       if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcReq->cellId))
240       {
241          cellCb = schCb[inst].cells[cellIdx];
242          break;
243       }
244    }
245    
246    if(cellCb)
247    {
248       /* Fetch UE CB */
249       ueCb = schGetUeCb(cellCb, schRachRsrcReq->crnti);
250       if(ueCb->crnti != schRachRsrcReq->crnti)
251       {
252          DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found" ,schRachRsrcReq->crnti);
253          rachRsrcRsp->result = RSP_NOK;
254       }
255    }
256    else
257    {
258       DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found" ,schRachRsrcReq->cellId);
259       rachRsrcRsp->result = RSP_NOK;
260    }
261
262    /* Allocate SSB resource if no failure has occurred until this step */
263    if(rachRsrcRsp->result == RSP_OK)
264    {
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;
271
272       /* Allocate resource for each SSB index requested */
273       for(ssbIdx = 0; ssbIdx < schRachRsrcReq->numSsb; ssbIdx++)
274       {
275          /* Find the first CF Preamble index not dedicated to any UE currently */
276          while(firstCFPreambleIndex <= lastCFPreambleIndex)
277          {
278             mask = 1 << firstCFPreambleIndex;
279             if(cellCb->dedPreambleBitMap & mask)
280             {
281                firstCFPreambleIndex++;
282                continue;
283             }
284             else
285                break;
286          }
287
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)
295          {
296             ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx = schRachRsrcReq->ssbIdx[ssbIdx]; 
297             ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx = firstCFPreambleIndex;
298             SET_ONE_BIT(firstCFPreambleIndex, cellCb->dedPreambleBitMap);
299             cfraSsbIdx++;
300             firstCFPreambleIndex++;
301          }
302          else
303          {
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 */
307             break;
308          }
309       } /* End of for */
310
311       ueCb->cfraResource.numSsb = cfraSsbIdx;
312
313       if(ueCb->cfraResource.numSsb == 0)
314       {
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;
318       }
319       else
320       {   
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));
325       }
326    } /* End of if */
327
328    /* Free RACH resource request memory allocated by MAC */
329    SCH_FREE(schRachRsrcReq, sizeof(SchRachRsrcReq));
330
331    /* Send RACH resource response to MAC */
332    return(MacMessageRouter(&rspPst, (void *)rachRsrcRsp));
333 }
334
335 /**
336  * @brief calculate ra-rnti function. 
337  *
338  * @details
339  *
340  *     Function : calculateRaRnti
341  *     
342  *     This function calculates ra-rnti
343  *     
344  *  @param[in]  symbol index
345  *  @param[in]  slot index
346  *  @param[in]  frequency index
347  *  @return  ra-rnti
348  **/
349 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
350 {
351    uint16_t raRnti = 0;
352    uint8_t  ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
353    
354    /* Refer to spec 38.321, section 5.1.3 */
355    raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
356    return raRnti;
357 }
358
359 /**
360  * @brief create raCb function. 
361  *
362  * @details
363  *
364  *     Function : createSchRaCb
365  *     
366  *     This function create raCb
367  *     
368  *  @param[in]  crnti
369  *  @param[in]  shed instance
370  *  @return  void
371  **/
372 void createSchRaCb(uint8_t ueId, SchRaReq *raReq, Inst schInst)
373 {
374    if(raReq->isCFRA)
375    {
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))
379       {
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;
384       }
385    }
386    else
387    {
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;
393    }
394    schCb[schInst].cells[schInst]->raCb[ueId -1].cell = schCb[schInst].cells[schInst];
395 }
396
397 /**
398  * @brief resource allocation for msg3 PUSCH
399  *
400  * @details
401  *
402  *     Function : schAllocMsg3Pusch 
403  *     
404  *     This function handles msg3 PUSCH allocation
405  *     
406  *  @param[in]  Inst schInst, SCH instance
407  *  @param[in]  slot, current slot
408  *  @param[out]  msg3StartRb
409  *  @param[out]  msg3NumRb
410  *  @return  void
411  **/
412 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime, SchUlHqProcCb* msg3HqProc, bool isRetx)
413 {
414    SchCellCb      *cell          = NULLP;
415    SchUlSlotInfo  *schUlSlotInfo = NULLP;
416    uint8_t    mcs       = DEFAULT_MCS;
417    uint8_t    startSymb = 0;
418    uint8_t    symbLen   = 0; 
419    uint16_t   startRb   = 0;
420    uint16_t   numRb     = 0;
421    uint16_t   tbSize    = 0;
422
423    cell = schCb[schInst].cells[schInst];
424    if(cell == NULL)
425    {
426       DU_LOG("\n\nERROR  -->  SCH :  Failed to find cell in schAllocMsg3Pusch");
427       return NULLP;
428    }
429
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;
433
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);
439
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)
444    {
445       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
446       return NULLP;
447    }
448
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*/
452
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;
465 #ifdef INTEL_FAPI   
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;
469 #endif   
470    
471    if(!isRetx)
472    {
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;
484 #ifdef INTEL_FAPI      
485       msg3HqProc->dmrsMappingType = schUlSlotInfo->schPuschInfo->dmrsMappingType;
486       msg3HqProc->nrOfDmrsSymbols = schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols;
487       msg3HqProc->dmrsAddPos = schUlSlotInfo->schPuschInfo->dmrsAddPos;
488 #endif
489    }
490    return schUlSlotInfo->schPuschInfo;
491 }
492
493 /**
494  * @brief Check if a time frame is in RA Response window
495  *
496  * @details
497  *
498  *     Function : isInRaRspWindow
499  *
500  *     Check if a time frame is in RA Response window
501  *
502  *  @param[in]  RA request
503  *  @param[in]  Time frame to check
504  *  @param[in]  Total number of slot per radio frame
505  *  @return  true 
506  *  @return  false
507  **/
508 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
509 {
510    uint32_t winStartTime, winEndTime, timeToCheck;
511    
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;
515
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;
521       
522    return WINDOW_EXPIRED;
523 }
524
525 /**
526  * @brief Processes any pending RA request
527  *
528  * @details
529  *
530  *     Function : schProcessRaReq
531  *
532  *     This function process pending RA request
533  *
534  *  @param[in]  Current timing of the cell
535  *  @return  ROK
536  **/
537 bool schProcessRaReq(Inst schInst, SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
538 {
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;
543    uint8_t   numK1 = 0;
544    uint8_t   puschMu = 0;
545    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
546 #ifdef NR_TDD
547    uint8_t   totalCfgSlot = 0;
548 #endif
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;
557    
558 #ifdef NR_TDD
559    totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbScs, cell->cellCfg.tddCfg.tddPeriod);
560 #endif
561    k0K1InfoTbl    = &cell->k0K1InfoTbl;
562    if(cell->raReq[ueId-1]->isCFRA == false)
563    {
564       msg3K2InfoTbl  = &cell->msg3K2InfoTbl;
565       puschMu        = cell->numerology;
566       msg3Delta      = puschDeltaTable[puschMu];
567       msg3MinSchTime = minMsg3SchTime[cell->numerology];
568    }
569
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;
573 #ifdef NR_TDD
574    /* Consider this slot for sending DCI, only if it is a DL slot */
575    if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
576 #endif
577    {
578       /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
579       if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
580          return false;
581
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)
585       {
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. 
589           */
590          for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
591          {
592             k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
593             k0 = cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
594
595             /* Calculating time frame to send RAR PDSCH */
596             ADD_DELTA_TO_TIME(dciTime, rarTime, k0, cell->numSlots);
597             rarSlot = rarTime.slot;
598             
599             /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
600             if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
601                continue;
602
603             /* If Contention-FREE RA is in progress, allocate resources for
604              * PUCCH for next UL message */
605             if(cell->raReq[ueId-1]->isCFRA)
606             {
607                numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
608                for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
609                {   
610                   k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
611                   if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck)
612                   {
613                      k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck->\
614                         dlDataToUlAckList[k1Index];
615                   }
616                   else
617                   {
618                      k1 = defaultUlAckTbl[k1Index];
619                   }
620
621                   ADD_DELTA_TO_TIME(rarTime, pucchTime, k1, cell->numSlots);
622 #ifdef NR_TDD
623                   if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
624                      continue;
625 #endif
626                   if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
627                      continue;
628                   k1Found = true;
629                   break;
630                }
631             }
632             else
633             {
634                /* Else if contention-based RA is in progress, allocate resources for MSG3 */
635                for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
636                {
637                   k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
638                   k2 = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
639
640                   /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
641                   k2 = k2 + msg3Delta;
642                   if(k2 >= msg3MinSchTime)
643                   {
644                      ADD_DELTA_TO_TIME(rarTime, msg3Time, k2, cell->numSlots);
645 #ifdef NR_TDD
646                      if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
647                         continue;
648 #endif
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)
652                         continue;
653
654                      k2Found = true;
655                      break;
656                   }
657                }
658             }
659             if(k1Found || k2Found)
660                break;
661          }
662       }
663       else if(windowStatus == WINDOW_EXPIRED)
664       {
665          SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
666          SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
667          return false;
668       }
669
670       /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
671       if(!k1Found && !k2Found)
672          return false;
673
674       /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
675       SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
676       if(dciSlotAlloc == NULLP)
677       {
678          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
679          return false;
680       }
681       cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
682
683       /* Fill PDCCH and PDSCH scheduling information for RAR */
684       if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
685       {
686          DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
687          if(!dciSlotAlloc->rarPdschCfg)
688          {
689             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
690             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
691          }
692          return false;
693       }
694
695       /* Fill RAR info */
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;
700
701       if(cell->raReq[ueId-1]->isCFRA)
702       {
703          /* Allocate resources for PUCCH */
704          schAllocPucchResource(cell, pucchTime, cell->raReq[ueId-1]->rachInd->crnti,NULLP, FALSE, NULLP);
705       }
706       else
707       {
708          /* Allocate resources for msg3 */
709          msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time, &(cell->raCb[ueId-1].msg3HqProc), FALSE);
710          if(msg3PuschInfo)
711          {
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;
723          }
724       }
725
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
728        */
729       if(dciSlot == rarSlot)
730       {
731          SCH_ALLOC(dciSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
732          if(dciSlotAlloc->rarPdschCfg)
733          {
734              memcpy(dciSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci[0].pdschCfg, sizeof(PdschCfg));
735          }
736          else
737          {
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");
742             return false;
743          }
744
745       }
746       else
747       {
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)
751          {
752             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
753             SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
754             if(!dciSlotAlloc->rarPdschCfg)
755             {
756                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
757                cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
758             }
759             return false;
760          }
761          cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
762
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)
768          {
769             memcpy(rarSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci[0].pdschCfg,sizeof(PdschCfg));
770          }
771          else
772          {
773             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc->rarPdschCfg");
774             SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
775             if(!dciSlotAlloc->rarPdschCfg)
776             {
777                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
778                cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
779             }
780             SCH_FREE(rarSlotAlloc, sizeof(RarAlloc));
781             cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = NULLP;
782             return false;
783          }
784       }
785
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;
790       else
791          cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
792
793       /* Create raCb at SCH */
794       createSchRaCb(ueId, cell->raReq[ueId-1], schInst);
795
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));
799       
800       return true;
801    }
802    return false;
803 }
804
805 /**
806  * @brief process rach indication function. 
807  *
808  * @details
809  *
810  *     Function : SchProcRachInd
811  *     
812  *     This function process rach indication
813  *     
814  *  @param[in]  rachInd parameters
815  *  @param[in]  shed instance
816  *  @return  ROK
817  **/
818 uint8_t SchProcRachInd(Pst *pst, RachIndInfo *rachInd)
819 {
820    SchRaReq  *raReq = NULLP;
821    float     slotDuration;
822    uint8_t   winNumSlots;
823    uint8_t   ueId;
824    Inst      schInst = pst->dstInst-SCH_INST_START;
825    SchCellCb *cell = schCb[schInst].cells[schInst];
826
827    DU_LOG("\nINFO  -->  SCH : Received Rach indication");
828
829    if(cell == NULLP)
830    {
831       DU_LOG("\nERROR  -->  SCH: Failed to find cell in SchProcRachInd");
832       return RFAILED;
833    }
834
835    /* Storing RA request in cellCb */
836    GET_UE_ID(rachInd->crnti, ueId);
837    if(ueId <= 0)
838    {
839       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
840       return RFAILED;
841    }
842
843    SCH_ALLOC(raReq, sizeof(SchRaReq));
844    if(!raReq)
845    {
846       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in SchProcRachInd");
847       SCH_FREE(rachInd, sizeof(RachIndInfo));
848       return RFAILED;
849    }
850
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))
855    {
856       raReq->isCFRA = true;
857       raReq->ueCb = &cell->ueCb[ueId-1];
858    }
859    else
860       raReq->isCFRA = false;
861    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
862    raReq->winStartTime.slot = rachInd->timingInfo.slot;
863   
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;
867    
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;
871
872    /* Adding UE Id to list of pending UEs to be scheduled */
873    cell->api->SchRachInd(cell, ueId);
874    return ROK;
875 }
876
877 /**
878  * @brief fill RAR info function. 
879  *
880  * @details
881  *
882  *     Function : calculateRaRnti
883  *     
884  *     This function fills pdcch and pdsch info for RAR
885  *     
886  *  @param[in]  rar Allocation info
887  *  @param[in]  ra-rnti
888  *  @param[in]  PCI
889  *  @param[in]  offset to pointA to determine freq alloc
890  *  @return  ROK
891  **/
892 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
893 {
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 ;
898    uint16_t numRbs = 0;
899    uint16_t tbSize = 0;
900    PdschCfg *pdsch;
901
902    SchBwpDlCfg *initialBwp = &cell->cellCfg.dlCfgCommon.schInitialDlBwp;
903    SCH_ALLOC(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
904    if(rarAlloc->rarPdcchCfg == NULLP)
905    {
906       DU_LOG("\nERROR  --> SCH : Memory allocation failed in %s",__func__);
907       return RFAILED;
908    }
909    PdcchCfg *pdcch = rarAlloc->rarPdcchCfg;
910    BwpCfg *bwp = &rarAlloc->bwp;
911
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];
916
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++)
921    {
922       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
923          break;
924       else
925          mask = mask>>1;
926    }
927
928    /* fill BWP */
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;
933
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);
939
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 */
947    pdcch->numDlDci = 1;
948    pdcch->dci[0].rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
949    pdcch->dci[0].scramblingId = cell->cellCfg.phyCellId;
950    pdcch->dci[0].scramblingRnti = 0;
951    pdcch->dci[0].cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
952    pdcch->dci[0].aggregLevel = 4;
953    pdcch->dci[0].beamPdcchInfo.numPrgs = 1;
954    pdcch->dci[0].beamPdcchInfo.prgSize = 1;
955    pdcch->dci[0].beamPdcchInfo.digBfInterfaces = 0;
956    pdcch->dci[0].beamPdcchInfo.prg[0].pmIdx = 0;
957    pdcch->dci[0].beamPdcchInfo.prg[0].beamIdx[0] = 0;
958    pdcch->dci[0].txPdcchPower.beta_pdcch_1_0 = 0;
959    pdcch->dci[0].txPdcchPower.powerControlOffsetSS = 0;
960
961    pdsch = &pdcch->dci[0].pdschCfg;
962    /* fill the PDSCH PDU */
963    uint8_t cwCount = 0;
964    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
965    pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
966    pdsch->pduIndex = 0;
967    pdsch->numCodewords = 1;
968    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
969    {
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;
978    }
979    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
980    pdsch->numLayers = 1;
981    pdsch->transmissionScheme = 0;
982    pdsch->refPoint = 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;
992
993    pdsch->pdschTimeAlloc.rowIndex = k0Index;
994    pdsch->pdschTimeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
995    pdsch->pdschTimeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
996
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);
1002
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)
1008    {
1009       startSymbol = pdsch->pdschTimeAlloc.startSymb;
1010       numSymbol = pdsch->pdschTimeAlloc.numSymb;
1011    }
1012    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
1013    else
1014    {
1015       startSymbol = dmrsStartSymbol;
1016       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
1017    }
1018
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)
1022    {
1023       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
1024       SCH_FREE(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
1025       return RFAILED;
1026    }
1027
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;
1035
1036    return ROK;
1037 }
1038
1039  /* @brief Process RACH resource release after CFRA
1040  *
1041  * @details
1042  *
1043  *     Function : SchProcRachRsrcRel
1044  *     
1045  *     This function processes RACH resorce release
1046  *     from MAC after CFRA. It releases the dedicated 
1047  *     preamble alloted to the UE
1048  *     
1049  *  @param[in]  Post structure
1050  *  @param[in]  RACH resource release
1051  *  @return     ROK
1052  *              RFAILED
1053  */
1054 uint8_t SchProcRachRsrcRel(Pst *pst, SchRachRsrcRel *schRachRsrcRel)
1055 {
1056    uint8_t      ret = ROK;
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;
1062
1063    DU_LOG("\nINFO  -->  SCH : Received RACH resource release for Cell ID [%d] CRNTI [%d]", \
1064          schRachRsrcRel->cellId, schRachRsrcRel->crnti);
1065
1066    /* Fetch Cell CB */
1067    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1068    {
1069       if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcRel->cellId))
1070       {
1071          cellCb = schCb[inst].cells[cellIdx];
1072          break;
1073       }
1074    }
1075    
1076    if(cellCb)
1077    {
1078       /* Fetch UE CB */
1079       ueCb = schGetUeCb(cellCb, schRachRsrcRel->crnti);
1080       if(ueCb->crnti != schRachRsrcRel->crnti)
1081       {
1082          DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found", schRachRsrcRel->crnti);
1083          ret = RFAILED;
1084       }
1085    }
1086    else
1087    {
1088       DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found", schRachRsrcRel->cellId);
1089       ret = RFAILED;
1090    }
1091
1092    /* Free SSB resource if no failure has occurred until this step */
1093    if(ret == ROK)
1094    {
1095       for(ssbIdx = 0; ssbIdx < schRachRsrcRel->cfraResource.numSsb; ssbIdx++)
1096       {
1097          /* Search each ssbIdx entry in UE Cb */
1098          for(cfraSsbIdx = 0; cfraSsbIdx < ueCb->cfraResource.numSsb; cfraSsbIdx++)
1099          {
1100             if(ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx == schRachRsrcRel->cfraResource.ssbResource[ssbIdx].ssbIdx)
1101             {
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--;
1107                break;
1108             }
1109          }
1110       } /* End of for */
1111    } /* End of if */
1112
1113    /* Free RACH resource release memory allocated by MAC */
1114    SCH_FREE(schRachRsrcRel, sizeof(SchRachRsrcRel));
1115    return ret;
1116 }
1117  /* @brief process MSG4 completion
1118  *
1119  * @details
1120  *
1121  *     Function : schMsg4Complete
1122  *     
1123  *     This function updates ra state and msg4 Hqrq 
1124  *     proc upon MSG4 completion     
1125  *  @param[in]  SchUeCb *ueCb, UE cb pointer
1126  *  @return     VOID
1127  */
1128 void schMsg4Complete(SchUeCb *ueCb)
1129 {
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;
1133 }
1134 /**********************************************************************
1135          End of file
1136 **********************************************************************/