c07a7261a920044e2f5b61b5cd6273b67e3a70f4
[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, ret = OK;
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 Contention-FREE RA is in progress, allocate resources for
600              * PUCCH for next UL message */
601             if(cell->raReq[ueId-1]->isCFRA)
602             {
603                numK1 = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.numK1;
604                for(k1TblIdx = 0; k1TblIdx < numK1; k1TblIdx++)
605                {   
606                   k1Index = k0K1InfoTbl->k0k1TimingInfo[dciTime.slot].k0Indexes[k0TblIdx].k1TimingInfo.k1Indexes[k1TblIdx];
607                   if(cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck)
608                   {
609                      k1 = cell->raReq[ueId-1]->ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg.dlDataToUlAck->\
610                         dlDataToUlAckList[k1Index];
611                   }
612                   else
613                   {
614                      k1 = defaultUlAckTbl[k1Index];
615                   }
616
617                   ADD_DELTA_TO_TIME(rarTime, pucchTime, k1, cell->numSlots);
618 #ifdef NR_TDD
619                   if(schGetSlotSymbFrmt(pucchTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
620                      continue;
621 #endif
622                   if(cell->schUlSlotInfo[pucchTime.slot]->pucchUe != 0)
623                      continue;
624                   k1Found = true;
625                   break;
626                }
627             }
628             else
629             {
630                /* Else if contention-based RA is in progress, allocate resources for MSG3 */
631                for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
632                {
633                   k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
634                   k2 = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
635
636                   /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
637                   k2 = k2 + msg3Delta;
638                   if(k2 >= msg3MinSchTime)
639                   {
640                      ADD_DELTA_TO_TIME(rarTime, msg3Time, k2, cell->numSlots);
641 #ifdef NR_TDD
642                      if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
643                         continue;
644 #endif
645                      /* If PUSCH is already scheduled on this slot, another PUSCH
646                       * pdu cannot be scheduled here */
647                      if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
648                         continue;
649
650                      k2Found = true;
651                      break;
652                   }
653                }
654             }
655             if(k1Found || k2Found)
656                break;
657          }
658       }
659       else if(windowStatus == WINDOW_EXPIRED)
660       {
661          SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
662          SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
663          return false;
664       }
665
666       /* If K0-K2 and K0-K1 combination not found, no scheduling happens */
667       if(!k1Found && !k2Found)
668          return false;
669
670       /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
671       SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
672       if(dciSlotAlloc == NULLP)
673       {
674          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
675          return false;
676       }
677       cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
678
679       /* Fill PDCCH and PDSCH scheduling information for RAR */
680       if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
681       {
682          DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
683          if(!dciSlotAlloc->rarPdschCfg)
684          {
685             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
686             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
687          }
688          return false;
689       }
690
691       /* Fill RAR info */
692       dciSlotAlloc->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          cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
701          ret = schAllocPucchResource(cell, pucchTime, NULLP, NULLP, NULLP);
702          if(ret == RFAILED)
703          {
704             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
705             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
706             DU_LOG("\nERROR  -->  SCH : Resource allocation for PUCCH failed for CFRA!");
707             return false;
708             
709          }
710       }
711       else
712       {
713          cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
714          /* Allocate resources for msg3 */
715          msg3PuschInfo = schAllocMsg3Pusch(schInst, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time, &(cell->raCb[ueId-1].msg3HqProc), FALSE);
716          if(msg3PuschInfo)
717          {
718             dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp.freqAlloc.numPrb;
719             /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
720             dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
721             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.startPrb;
722             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.resAlloc.type1.numPrb;
723             dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
724             dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
725             dciSlotAlloc->rarInfo.ulGrant.tpc = 3;  /* TODO : Check appropriate value to be filled */
726             /* Spec 38.213, section 8.2 : In a contention based random access
727              * procedure, the CSI request field is reserved. */
728             dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
729          }
730       }
731
732       /* Check if both DCI and RAR are sent in the same slot.
733        * If not, allocate memory RAR PDSCH slot to store RAR info
734        */
735       if(dciSlot == rarSlot)
736       {
737          SCH_ALLOC(dciSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
738          if(dciSlotAlloc->rarPdschCfg)
739          {
740              memcpy(dciSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci[0].pdschCfg, sizeof(PdschCfg));
741          }
742          else
743          {
744             SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
745             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
746             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
747             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc->rarPdschCfg");
748             return false;
749          }
750
751       }
752       else
753       {
754          /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
755          SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
756          if(rarSlotAlloc == NULLP)
757          {
758             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
759             SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
760             if(!dciSlotAlloc->rarPdschCfg)
761             {
762                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
763                cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
764             }
765             return false;
766          }
767          cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
768
769          /* Copy all RAR info */
770          rarSlotAlloc->raRnti = dciSlotAlloc->raRnti;
771          rarSlotAlloc->bwp = dciSlotAlloc->bwp;
772          SCH_ALLOC(rarSlotAlloc->rarPdschCfg, sizeof(PdschCfg));
773          if(rarSlotAlloc->rarPdschCfg)
774          {
775             memcpy(rarSlotAlloc->rarPdschCfg, &dciSlotAlloc->rarPdcchCfg->dci[0].pdschCfg,sizeof(PdschCfg));
776          }
777          else
778          {
779             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc->rarPdschCfg");
780             SCH_FREE(dciSlotAlloc->rarPdcchCfg, sizeof(PdcchCfg));
781             if(!dciSlotAlloc->rarPdschCfg)
782             {
783                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
784                cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
785             }
786             SCH_FREE(rarSlotAlloc, sizeof(RarAlloc));
787             cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = NULLP;
788             return false;
789          }
790       }
791
792       cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
793
794       /* Create raCb at SCH */
795       createSchRaCb(ueId, cell->raReq[ueId-1], schInst);
796
797       /* Remove RachInd from pending RA request list */
798       SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
799       SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
800       
801       return true;
802    }
803    return false;
804 }
805
806 /**
807  * @brief process rach indication function. 
808  *
809  * @details
810  *
811  *     Function : SchProcRachInd
812  *     
813  *     This function process rach indication
814  *     
815  *  @param[in]  rachInd parameters
816  *  @param[in]  shed instance
817  *  @return  ROK
818  **/
819 uint8_t SchProcRachInd(Pst *pst, RachIndInfo *rachInd)
820 {
821    SchRaReq  *raReq = NULLP;
822    float     slotDuration;
823    uint8_t   winNumSlots;
824    uint8_t   ueId;
825    Inst      schInst = pst->dstInst-SCH_INST_START;
826    SchCellCb *cell = schCb[schInst].cells[schInst];
827
828    DU_LOG("\nINFO  -->  SCH : Received Rach indication");
829
830    if(cell == NULLP)
831    {
832       DU_LOG("\nERROR  -->  SCH: Failed to find cell in SchProcRachInd");
833       return RFAILED;
834    }
835
836    /* Storing RA request in cellCb */
837    GET_UE_ID(rachInd->crnti, ueId);
838    if(ueId <= 0)
839    {
840       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
841       return RFAILED;
842    }
843
844    SCH_ALLOC(raReq, sizeof(SchRaReq));
845    if(!raReq)
846    {
847       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in SchProcRachInd");
848       SCH_FREE(rachInd, sizeof(RachIndInfo));
849       return RFAILED;
850    }
851
852    /* calculate the ra-rnti value */
853    raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
854    raReq->rachInd = rachInd;
855    if((cell->ueCb[ueId-1].crnti == rachInd->crnti) && (cell->ueCb[ueId-1].state == SCH_UE_HANDIN_IN_PROGRESS))
856    {
857       raReq->isCFRA = true;
858       raReq->ueCb = &cell->ueCb[ueId-1];
859    }
860    else
861       raReq->isCFRA = false;
862    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
863    raReq->winStartTime.slot = rachInd->timingInfo.slot;
864   
865    /* Converting window size from ms to number of slots */
866    slotDuration = (1 / pow(2, cell->numerology));
867    winNumSlots = (float)cell->cellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.raRspWindow / slotDuration;
868    
869    /* Adding window size to window start time to get window end time */
870    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots, cell->numSlots);
871    cell->raReq[ueId -1] = raReq;
872
873    /* Adding UE Id to list of pending UEs to be scheduled */
874    cell->api->SchRachInd(cell, ueId);
875    return ROK;
876 }
877
878 /**
879  * @brief fill RAR info function. 
880  *
881  * @details
882  *
883  *     Function : calculateRaRnti
884  *     
885  *     This function fills pdcch and pdsch info for RAR
886  *     
887  *  @param[in]  rar Allocation info
888  *  @param[in]  ra-rnti
889  *  @param[in]  PCI
890  *  @param[in]  offset to pointA to determine freq alloc
891  *  @return  ROK
892  **/
893 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
894 {
895    uint8_t  coreset0Idx = 0;
896    uint8_t  firstSymbol = 0, numSymbols = 0;
897    uint8_t  mcs = DEFAULT_MCS;  /* MCS fixed to 4 */
898    uint8_t  dmrsStartSymbol, startSymbol, numSymbol ;
899    uint16_t numRbs = 0;
900    uint16_t tbSize = 0;
901    PdschCfg *pdsch;
902
903    SchBwpDlCfg *initialBwp = &cell->cellCfg.dlCfgCommon.schInitialDlBwp;
904    SCH_ALLOC(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
905    if(rarAlloc->rarPdcchCfg == NULLP)
906    {
907       DU_LOG("\nERROR  --> SCH : Memory allocation failed in %s",__func__);
908       return RFAILED;
909    }
910    PdcchCfg *pdcch = rarAlloc->rarPdcchCfg;
911    BwpCfg *bwp = &rarAlloc->bwp;
912
913    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
914    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
915    numRbs     = coresetIdxTable[coreset0Idx][1]; 
916    numSymbols = coresetIdxTable[coreset0Idx][2];
917
918    /* calculate time domain parameters */
919    // note: since slot value is made sl1, RAR can be sent at all slots
920    uint16_t mask = 0x2000;
921    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
922    {
923       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
924          break;
925       else
926          mask = mask>>1;
927    }
928
929    /* fill BWP */
930    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
931    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
932    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
933    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
934
935    /* fill the PDCCH PDU */
936    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
937    pdcch->coresetCfg.durationSymbols = numSymbols;
938    memcpy(pdcch->coresetCfg.freqDomainResource, \
939       cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
940
941    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
942    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
943    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
944    pdcch->coresetCfg.coreSetType = 0;
945    pdcch->coresetCfg.coreSetSize = numRbs;
946    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
947    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
948    pdcch->numDlDci = 1;
949    pdcch->dci[0].rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
950    pdcch->dci[0].scramblingId = cell->cellCfg.phyCellId;
951    pdcch->dci[0].scramblingRnti = 0;
952    pdcch->dci[0].cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
953    pdcch->dci[0].aggregLevel = 4;
954    pdcch->dci[0].beamPdcchInfo.numPrgs = 1;
955    pdcch->dci[0].beamPdcchInfo.prgSize = 1;
956    pdcch->dci[0].beamPdcchInfo.digBfInterfaces = 0;
957    pdcch->dci[0].beamPdcchInfo.prg[0].pmIdx = 0;
958    pdcch->dci[0].beamPdcchInfo.prg[0].beamIdx[0] = 0;
959    pdcch->dci[0].txPdcchPower.beta_pdcch_1_0 = 0;
960    pdcch->dci[0].txPdcchPower.powerControlOffsetSS = 0;
961
962    pdsch = &pdcch->dci[0].pdschCfg;
963    /* fill the PDSCH PDU */
964    uint8_t cwCount = 0;
965    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
966    pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
967    pdsch->pduIndex = 0;
968    pdsch->numCodewords = 1;
969    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
970    {
971       pdsch->codeword[cwCount].targetCodeRate = 308;
972       pdsch->codeword[cwCount].qamModOrder = 2;
973       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
974       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
975       pdsch->codeword[cwCount].rvIndex = 0;
976       /* RAR PDU length and FAPI payload header length */
977       tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
978       pdsch->codeword[cwCount].tbSize = tbSize;
979    }
980    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
981    pdsch->numLayers = 1;
982    pdsch->transmissionScheme = 0;
983    pdsch->refPoint = 0;
984    pdsch->dmrs.dlDmrsSymbPos = DL_DMRS_SYMBOL_POS; 
985    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
986    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
987    pdsch->dmrs.scid = 0;
988    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
989    pdsch->dmrs.dmrsPorts = 0;
990    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
991    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
992    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
993
994    pdsch->pdschTimeAlloc.rowIndex = k0Index;
995    pdsch->pdschTimeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
996    pdsch->pdschTimeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
997
998    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
999    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
1000    pdsch->pdschFreqAlloc.startPrb = MAX_NUM_RB;
1001    pdsch->pdschFreqAlloc.numPrb = \
1002       schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
1003
1004    /* Find total symbols occupied including DMRS */
1005    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
1006    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT, 
1007     * in that case only PDSCH symbols are marked as occupied */
1008    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
1009    {
1010       startSymbol = pdsch->pdschTimeAlloc.startSymb;
1011       numSymbol = pdsch->pdschTimeAlloc.numSymb;
1012    }
1013    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
1014    else
1015    {
1016       startSymbol = dmrsStartSymbol;
1017       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
1018    }
1019
1020    /* Allocate the number of PRBs required for RAR PDSCH */
1021    if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
1022       &pdsch->pdschFreqAlloc.startPrb, pdsch->pdschFreqAlloc.numPrb)) != ROK)
1023    {
1024       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
1025       SCH_FREE(rarAlloc->rarPdcchCfg, sizeof(PdcchCfg));
1026       return RFAILED;
1027    }
1028
1029    pdsch->beamPdschInfo.numPrgs = 1;
1030    pdsch->beamPdschInfo.prgSize = 1;
1031    pdsch->beamPdschInfo.digBfInterfaces = 0;
1032    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
1033    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
1034    pdsch->txPdschPower.powerControlOffset = 0;
1035    pdsch->txPdschPower.powerControlOffsetSS = 0;
1036
1037    return ROK;
1038 }
1039
1040  /* @brief Process RACH resource release after CFRA
1041  *
1042  * @details
1043  *
1044  *     Function : SchProcRachRsrcRel
1045  *     
1046  *     This function processes RACH resorce release
1047  *     from MAC after CFRA. It releases the dedicated 
1048  *     preamble alloted to the UE
1049  *     
1050  *  @param[in]  Post structure
1051  *  @param[in]  RACH resource release
1052  *  @return     ROK
1053  *              RFAILED
1054  */
1055 uint8_t SchProcRachRsrcRel(Pst *pst, SchRachRsrcRel *schRachRsrcRel)
1056 {
1057    uint8_t      ret = ROK;
1058    uint8_t      ssbIdx = 0, cfraSsbIdx = 0;
1059    uint16_t     cellIdx = 0;
1060    Inst         inst = pst->dstInst - SCH_INST_START;
1061    SchCellCb    *cellCb = NULLP;
1062    SchUeCb      *ueCb = NULLP;
1063
1064    DU_LOG("\nINFO  -->  SCH : Received RACH resource release for Cell ID [%d] CRNTI [%d]", \
1065          schRachRsrcRel->cellId, schRachRsrcRel->crnti);
1066
1067    /* Fetch Cell CB */
1068    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1069    {
1070       if((schCb[inst].cells[cellIdx]) && (schCb[inst].cells[cellIdx]->cellId == schRachRsrcRel->cellId))
1071       {
1072          cellCb = schCb[inst].cells[cellIdx];
1073          break;
1074       }
1075    }
1076    
1077    if(cellCb)
1078    {
1079       /* Fetch UE CB */
1080       ueCb = schGetUeCb(cellCb, schRachRsrcRel->crnti);
1081       if(ueCb->crnti != schRachRsrcRel->crnti)
1082       {
1083          DU_LOG("\nERROR  -->  SCH : CRNTI [%d] not found", schRachRsrcRel->crnti);
1084          ret = RFAILED;
1085       }
1086    }
1087    else
1088    {
1089       DU_LOG("\nERROR  -->  SCH : Cell ID [%d] not found", schRachRsrcRel->cellId);
1090       ret = RFAILED;
1091    }
1092
1093    /* Free SSB resource if no failure has occurred until this step */
1094    if(ret == ROK)
1095    {
1096       for(ssbIdx = 0; ssbIdx < schRachRsrcRel->cfraResource.numSsb; ssbIdx++)
1097       {
1098          /* Search each ssbIdx entry in UE Cb */
1099          for(cfraSsbIdx = 0; cfraSsbIdx < ueCb->cfraResource.numSsb; cfraSsbIdx++)
1100          {
1101             if(ueCb->cfraResource.ssbResource[cfraSsbIdx].ssbIdx == schRachRsrcRel->cfraResource.ssbResource[ssbIdx].ssbIdx)
1102             {
1103                /* If ssbIdx entry is found in UE CB, free dedicated resources
1104                 * for this ssbIdx */
1105                UNSET_ONE_BIT(ueCb->cfraResource.ssbResource[cfraSsbIdx].raPreambleIdx, cellCb->dedPreambleBitMap);
1106                memset(&ueCb->cfraResource.ssbResource[cfraSsbIdx], 0, sizeof(SchCfraSsbResource));
1107                ueCb->cfraResource.numSsb--;
1108                break;
1109             }
1110          }
1111       } /* End of for */
1112    } /* End of if */
1113
1114    /* Free RACH resource release memory allocated by MAC */
1115    SCH_FREE(schRachRsrcRel, sizeof(SchRachRsrcRel));
1116    return ret;
1117 }
1118  /* @brief process MSG4 completion
1119  *
1120  * @details
1121  *
1122  *     Function : schMsg4Complete
1123  *     
1124  *     This function updates ra state and msg4 Hqrq 
1125  *     proc upon MSG4 completion     
1126  *  @param[in]  SchUeCb *ueCb, UE cb pointer
1127  *  @return     VOID
1128  */
1129 void schMsg4Complete(SchUeCb *ueCb)
1130 {
1131    DU_LOG("\nINFO --> SCH: State change for ueId[%2d] to SCH_RA_STATE_MSG4_DONE\n",ueCb->ueId);
1132    ueCb->cellCb->raCb[ueCb->ueId-1].raState = SCH_RA_STATE_MSG4_DONE;
1133    ueCb->msg4HqProc = ueCb->retxMsg4HqProc = NULLP;
1134 }
1135 /**********************************************************************
1136          End of file
1137 **********************************************************************/