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