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