c1dee263a6740ac2936b932e248c364111b8c876
[o-du/l2.git] / src / 5gnrsch / sch_common.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:     5G NR SCH layer
22
23 Type:     C source file
24
25 Desc:     C source code for Entry point fucntions
26
27 File:     sch_common.c
28
29  **********************************************************************/
30
31 /** @file sch_common.c
32   @brief This module performs common scheduling
33  */
34 #include "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37
38 #include "tfu.x"
39 #include "lrg.x"
40 #include "du_log.h"
41 #include "du_app_mac_inf.h"
42 #include "mac_sch_interface.h"
43 #include "sch.h"
44 #include "sch_tmr.h"
45 #include "sch_utils.h"
46
47 /**
48  * @brief common resource allocation for SSB
49  *
50  * @details
51  *
52  *     Function : schBroadcastSsbAlloc
53  *     
54  *     This function handles common scheduling for SSB
55  *     
56  *  @param[in]  SchCellCb *cell, cell cb
57  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
58  *  @return  void
59  **/
60 uint8_t schBroadcastSsbAlloc(SchCellCb *cell, SlotTimingInfo slotTime, DlBrdcstAlloc *dlBrdcstAlloc)
61 {
62    /* schedule SSB */
63    uint8_t ssbStartSymb, idx;
64    uint16_t ssbStartPrb;
65    SchDlSlotInfo *schDlSlotInfo;
66    SsbInfo ssbInfo;
67
68    if(cell == NULL)
69    {
70       DU_LOG("\nERROR  -->  SCH: schBroadcastSsbAlloc() : Cell is NULL");
71       return RFAILED;
72    }
73
74    if(dlBrdcstAlloc == NULL)
75    {
76       DU_LOG("\nERROR  -->  SCH: schBroadcastSsbAlloc() : dlBrdcstAlloc is NULL");
77       return RFAILED;
78    }
79
80    schDlSlotInfo = cell->schDlSlotInfo[slotTime.slot];
81    ssbStartPrb = cell->cellCfg.ssbSubcOffset; //+Kssb
82    ssbStartSymb = cell->ssbStartSymbArr[dlBrdcstAlloc->ssbIdxSupported-1]; /*since we are supporting only 1 ssb beam */
83
84    /* Assign interface structure */
85    for(idx=0; idx<dlBrdcstAlloc->ssbIdxSupported; idx++)
86    {
87       ssbInfo.ssbIdx              = idx;
88       ssbInfo.fdAlloc.startPrb    = ssbStartPrb;
89       ssbInfo.fdAlloc.numPrb      = SCH_SSB_NUM_PRB;
90       ssbInfo.tdAlloc.startSymb   = ssbStartSymb;
91       ssbInfo.tdAlloc.numSymb     = SCH_SSB_NUM_SYMB;
92       dlBrdcstAlloc->ssbInfo[idx] = ssbInfo;
93       schDlSlotInfo->ssbInfo[idx] = ssbInfo;
94    }
95
96    if((allocatePrbDl(cell, slotTime, ssbStartSymb, SCH_SSB_NUM_SYMB, &ssbInfo.fdAlloc.startPrb, ssbInfo.fdAlloc.numPrb)) != ROK)
97    {
98        DU_LOG("\nERROR  -->  SCH: PRB allocation failed for SSB in SFN:SLOT [%d : %d]", slotTime.sfn, slotTime.slot);
99        return RFAILED;
100    }
101
102
103    schDlSlotInfo->ssbPres = true;
104    schDlSlotInfo->ssbIdxSupported = dlBrdcstAlloc->ssbIdxSupported;
105    return ROK;
106 }
107
108 /**
109  * @brief common resource allocation for SIB1
110  *
111  * @details
112  *
113  *     Function : schBroadcastSib1Alloc
114  *     
115  *     This function handles common scheduling for SIB1
116  *     
117  *  @param[in]  SchCellCb *cell, cell cb
118  *  @param[in]  DlBrdcstAlloc *dlBrdcstAlloc, DL brdcst allocation
119  *  @return  void
120  **/
121 uint8_t schBroadcastSib1Alloc(SchCellCb *cell, SlotTimingInfo slotTime, DlBrdcstAlloc *dlBrdcstAlloc)
122 {
123    uint8_t dmrsStartSymbol, startSymbol, numSymbol ;
124    DmrsInfo dmrs;
125    PdschFreqAlloc freqAlloc;
126    PdschTimeAlloc timeAlloc;
127    SchDlSlotInfo *schDlSlotInfo = NULLP;
128
129    if(cell == NULL)
130    {
131       DU_LOG("\nERROR  -->  SCH: schBroadcastSsbAlloc() : Cell is NULL");
132       return RFAILED;
133    }
134
135    if(dlBrdcstAlloc == NULL)
136    {
137       DU_LOG("\nERROR  -->  SCH: schBroadcastSsbAlloc() : dlBrdcstAlloc is NULL");
138       return RFAILED;
139    }
140    
141    dlBrdcstAlloc->crnti = SI_RNTI;
142    dmrs = cell->sib1SchCfg.sib1PdcchCfg.dci.pdschCfg.dmrs;
143    freqAlloc = cell->sib1SchCfg.sib1PdcchCfg.dci.pdschCfg.pdschFreqAlloc;
144    timeAlloc = cell->sib1SchCfg.sib1PdcchCfg.dci.pdschCfg.pdschTimeAlloc;
145    schDlSlotInfo = cell->schDlSlotInfo[slotTime.slot];
146
147    /* Find total symbols used including DMRS */
148    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
149     * in that case only PDSCH symbols are marked as occupied */
150    dmrsStartSymbol = findDmrsStartSymbol(dmrs.dlDmrsSymbPos);   
151    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
152    {
153       startSymbol = timeAlloc.startSymb;
154       numSymbol = timeAlloc.numSymb;
155    }
156    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
157    else
158    {
159       startSymbol = dmrsStartSymbol;
160       numSymbol = dmrs.nrOfDmrsSymbols + timeAlloc.numSymb;
161    }
162
163    /* Allocate PRB */
164    if((allocatePrbDl(cell, slotTime, startSymbol, numSymbol, &freqAlloc.startPrb, freqAlloc.numPrb)) != ROK)
165    {
166        DU_LOG("\nERROR  -->  SCH: PRB allocation failed for SIB1 in SFN:Slot [%d : %d]", slotTime.sfn, slotTime.slot);
167        return RFAILED;
168    }
169
170    memcpy(&dlBrdcstAlloc->sib1Alloc.bwp, &cell->sib1SchCfg.bwp, sizeof(BwpCfg)); 
171    SCH_ALLOC(dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg,sizeof(PdcchCfg));
172    if(dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg)
173    {
174       memcpy(dlBrdcstAlloc->sib1Alloc.sib1PdcchCfg, &cell->sib1SchCfg.sib1PdcchCfg, sizeof(PdcchCfg)); 
175       schDlSlotInfo->sib1Pres = true;
176    }
177    else
178    {
179       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in %s", __func__);
180       return RFAILED;
181    }
182    return ROK;
183 }
184
185 /*******************************************************************
186  *
187  * @brief Handles sending UL scheduler info to MAC 
188  *
189  * @details
190  *
191  *    Function : sendUlSchInfoToMac
192  *
193  *    Functionality:
194  *     Sends UL Sch info to MAC from SCH
195  *
196  * @params[in] 
197  * @return ROK     - success
198  *         RFAILED - failure
199  *
200  * ****************************************************************/
201 int sendUlSchInfoToMac(UlSchedInfo *ulSchedInfo, Inst inst)
202 {
203    Pst pst;
204
205    memset(&pst, 0, sizeof(Pst));
206    FILL_PST_SCH_TO_MAC(pst, inst);
207    pst.event = EVENT_UL_SCH_INFO;
208
209    return(MacMessageRouter(&pst, (void *)ulSchedInfo));
210 }
211
212 /**
213  * @brief Function to fill Pucch Format 0
214  *
215  * @details
216  *
217  *     Function : fillPucchFormat0
218  *     
219  *     Function to fill Pucch format 0
220  *     
221  *  @param[in]  SchPucchInfo pointer, SchPucchResrcInfo pointer
222  *  @return  void
223  **/
224
225 void fillPucchFormat0(SchPucchInfo *ulSchedPucch, SchPucchResrcInfo *resrcInfo)
226 {
227    if(resrcInfo->SchPucchFormat.format0)
228    {
229       ulSchedPucch->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
230       ulSchedPucch->pucchFormat  = PUCCH_FORMAT_0;
231       ulSchedPucch->initialCyclicShift =  resrcInfo->SchPucchFormat.format0->initialCyclicShift;
232       ulSchedPucch->tdAlloc.numSymb = resrcInfo->SchPucchFormat.format0->numSymbols;
233       ulSchedPucch->tdAlloc.startSymb = resrcInfo->SchPucchFormat.format0->startSymbolIdx;
234    }
235 }
236
237 /**
238  * @brief Function to fill Pucch Format 1
239  *
240  * @details
241  *
242  *     Function : fillPucchFormat1
243  *     
244  *     Function to fill Pucch format 1
245  *     
246  *  @param[in]  SchPucchInfo pointer, SchPucchResrcInfo pointer
247  *  @return  void
248  **/
249
250 void fillPucchFormat1(SchPucchInfo *ulSchedPucch, SchPucchResrcInfo *resrcInfo)
251 {
252    if(resrcInfo->SchPucchFormat.format1)
253    {
254       ulSchedPucch->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
255       ulSchedPucch->pucchFormat  = PUCCH_FORMAT_1;
256       ulSchedPucch->initialCyclicShift =  resrcInfo->SchPucchFormat.format1->initialCyclicShift;
257       ulSchedPucch->tdAlloc.numSymb = resrcInfo->SchPucchFormat.format1->numSymbols;
258       ulSchedPucch->tdAlloc.startSymb = resrcInfo->SchPucchFormat.format1->startSymbolIdx;
259       ulSchedPucch->timeDomOCC =  resrcInfo->SchPucchFormat.format1->timeDomOCC;
260   }
261 }
262
263 /**
264  * @brief Function to fill Pucch format for UL Sched Info
265  *
266  * @details
267  *
268  *     Function : fillUlSchedPucchFormat
269  *     
270  *     Function to fill Pucch format for UL Sched Info
271  *     
272  *  @param[in]  pucchFormat , SchPucchInfo pointer,
273  *  @param[in]  SchPucchFormatCfg pointer, SchPucchResrcInfo pointer
274  *  @return  void
275  **/
276
277 uint8_t fillUlSchedPucchFormat(uint8_t pucchFormat, SchPucchInfo *ulSchedPucch,\
278    SchPucchResrcInfo *resrcInfo, SchPucchFormatCfg *formatCfg)
279 {
280    uint8_t ret = ROK;
281
282    switch(pucchFormat)
283    {
284       case PUCCH_FORMAT_0:
285          {
286             if(resrcInfo)
287                fillPucchFormat0(ulSchedPucch, resrcInfo);
288             return ret;
289          }
290       case PUCCH_FORMAT_1:
291          {
292             if(resrcInfo)
293             {
294                fillPucchFormat1(ulSchedPucch, resrcInfo);
295             }
296             if(formatCfg)
297             {
298                ulSchedPucch->addDmrs = formatCfg->addDmrs;
299                ulSchedPucch->pi2BPSK = formatCfg->pi2BPSK;
300             }
301             return ret;
302          }/* To Add support for more Pucch Format */
303
304       default:
305          DU_LOG("\nERROR  --> SCH : Invalid PUCCH format[%d] in fillUlSchedPucchFormatCfg()", pucchFormat);
306          ret = RFAILED;
307          return ret;
308    }
309    return ret;
310 }
311
312 /**
313  * @brief Function to fill Pucch Dedicated Cfg for UL Sched Info
314  *
315  * @details
316  *
317  *     Function : fillUlSchedPucchDedicatedCfg
318  *     
319  *     Function to fill Pucch Dedicated Cfg for UL Sched Info
320  *     
321  *  @param[in]  pucchFormat to be filled
322  *  @param[in]  SchPucchFormatCfg pointer, SchPucchCfg pointer
323  *  @return  void
324  **/
325
326 uint8_t fillUlSchedPucchDedicatedCfg(SchCellCb *cell, SchPucchCfg *pucchDedCfg,\
327    SlotTimingInfo *slotInfo, SchPucchInfo *ulSchedPucch)
328 {
329    uint8_t ret, resrcSetIdx, resrcIdx, schedReqIdx, srPeriodicity = 0;
330    uint16_t srOffset = 0;
331    uint16_t numSlots = cell->numSlots;
332    bool isAllocated = false;
333    uint16_t pucchStartPrb;
334    ret = ROK;
335    if(pucchDedCfg->resrcSet && pucchDedCfg->resrc)
336    {
337       //Assuming one entry in the list
338       for(resrcSetIdx = 0; resrcSetIdx < pucchDedCfg->resrcSet->resrcSetToAddModListCount; resrcSetIdx++)
339       {
340          for(resrcIdx = 0; resrcIdx < pucchDedCfg->resrc->resrcToAddModListCount; resrcIdx++)
341          {
342             if(pucchDedCfg->resrcSet->resrcSetToAddModList[resrcSetIdx].resrcList[resrcSetIdx] ==\
343                   pucchDedCfg->resrc->resrcToAddModList[resrcIdx].resrcId)
344             {
345                ulSchedPucch->intraFreqHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].intraFreqHop;
346                ulSchedPucch->secondPrbHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].secondPrbHop;
347                ulSchedPucch->fdAlloc.startPrb = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].startPrb;
348                ulSchedPucch->pucchFormat = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].pucchFormat;
349                ret = fillUlSchedPucchFormat(ulSchedPucch->pucchFormat, ulSchedPucch,\
350                      &pucchDedCfg->resrc->resrcToAddModList[resrcIdx], NULLP);
351                if(ret == RFAILED)
352                   return ret;
353
354                pucchStartPrb = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].startPrb;
355                ret = allocatePrbUl(cell, *slotInfo, ulSchedPucch->tdAlloc.startSymb, ulSchedPucch->tdAlloc.numSymb, &pucchStartPrb, PUCCH_NUM_PRB_FORMAT_0_1_4);
356                if(ret == ROK)
357                {
358                   isAllocated = true;
359                   break;
360                }
361             }
362          }
363          if(isAllocated)
364          break;
365       }
366    }
367
368    if(pucchDedCfg->format1)
369    {
370       ret = fillUlSchedPucchFormat(ulSchedPucch->pucchFormat, ulSchedPucch, NULLP, pucchDedCfg->format1);
371       if(ret == RFAILED)
372          return ret;
373    }
374
375    if(!isAllocated)
376    {
377       return RFAILED;
378    }
379
380    /* setting SR and UCI flag */
381    if(pucchDedCfg->schedReq)
382    {
383       for(schedReqIdx = 0; schedReqIdx < pucchDedCfg->schedReq->schedAddModListCount; schedReqIdx++)
384       {
385          srPeriodicity = pucchDedCfg->schedReq->schedAddModList[schedReqIdx].periodicity;
386          srOffset      = pucchDedCfg->schedReq->schedAddModList[schedReqIdx].offset;
387          break;
388       }
389       if(((numSlots * slotInfo->sfn + slotInfo->slot - srOffset) % srPeriodicity) == 0)
390       {
391          ulSchedPucch->srFlag  = true;
392       }
393    }
394    return ret;
395 }
396
397 /**
398  * @brief Function to fill Pucch Resource Info
399  *
400  * @details
401  *
402  *     Function : fillPucchResourceInfo
403  *     
404  *     Function to fill Pucch Resource Info
405  *     
406  *  @param[in]  SchPucchInfo *schPucchInfo, Inst inst
407  *  @return  ROK/RFAILED
408  **/
409
410 uint16_t fillPucchResourceInfo(uint8_t ueId, SchPucchInfo *schPucchInfo, Inst inst, SlotTimingInfo slotInfo)
411 {
412    uint8_t ret = ROK,  ueIdx = 0, pucchIdx = 0;
413    SchCellCb  *cell = schCb[inst].cells[inst];
414    SchPucchCfgCmn *pucchCfg = NULLP;
415    SchBwpParams *ulBwp = NULLP;
416 #ifdef NR_DRX 
417    SchUeCb *ueCb = NULLP;
418 #endif
419    uint16_t startPrb;
420
421    ueIdx = ueId -1;
422 #ifdef NR_DRX 
423    ueCb = &(cell->ueCb[ueIdx]); 
424    if(ueCb->ueDrxInfoPres)
425    {
426       if(!ueCb->drxUeCb.drxUlUeActiveStatus)
427          return RFAILED;
428    }
429 #endif
430    if(cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfgPres)
431    {
432       /* fill pucch dedicated cfg */
433       ret = fillUlSchedPucchDedicatedCfg(cell,\
434        &cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg, &slotInfo, schPucchInfo);
435       if(ret == RFAILED)
436       {
437          memset(schPucchInfo, 0, sizeof(SchPucchInfo));
438          DU_LOG("\nERROR  --> SCH : Filling PUCCH dedicated cfg failed at fillPucchResourceInfo()");
439          return ret;
440       }
441    }
442    else
443    {
444       /* fill pucch common cfg */
445       /* derive pucchResourceSet from schCellCfg */
446       pucchCfg = &cell->cellCfg.ulCfgCommon.schInitialUlBwp.pucchCommon;
447       pucchIdx = pucchCfg->pucchResourceCommon;
448       ulBwp = &cell->cellCfg.ulCfgCommon.schInitialUlBwp.bwp;
449       startPrb = ulBwp->freqAlloc.startPrb + pucchResourceSet[pucchIdx][3];
450       ret = allocatePrbUl(cell, slotInfo, pucchResourceSet[pucchIdx][1], pucchResourceSet[pucchIdx][2],\
451             &startPrb, PUCCH_NUM_PRB_FORMAT_0_1_4);
452       if (ret == ROK)
453       {
454          schPucchInfo->fdAlloc.startPrb = ulBwp->freqAlloc.startPrb + pucchResourceSet[pucchIdx][3];
455          schPucchInfo->fdAlloc.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
456          schPucchInfo->tdAlloc.startSymb = pucchResourceSet[pucchIdx][1];
457          schPucchInfo->tdAlloc.numSymb = pucchResourceSet[pucchIdx][2];
458          schPucchInfo->pucchFormat = pucchResourceSet[pucchIdx][0];
459
460          /* set SR and UCI flag to false */
461          schPucchInfo->srFlag  = true;
462       }
463    }
464    return ROK;
465 }
466
467 /**
468  * @brief resource allocation for UL
469  *
470  * @details
471  *
472  *     Function : schUlResAlloc
473  *     
474  *     This function handles UL Resource allocation
475  *     
476  *  @param[in]  SchCellCb *cell, cellCb
477  *  @return  void
478  **/
479 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst)
480 {
481    int ret = ROK;
482 #ifdef NR_DRX 
483    SchUeCb   *ueCb;
484 #endif
485    UlSchedInfo ulSchedInfo;
486    SchUlSlotInfo  *schUlSlotInfo = NULLP;
487    SlotTimingInfo ulTimingInfo;
488    CmLList        *node = NULLP;
489    TotalPrbUsage  *ulTotalPrbUsage = NULLP;
490
491    memset(&ulSchedInfo, 0, sizeof(UlSchedInfo));
492
493    /* add PHY delta */
494    ADD_DELTA_TO_TIME(cell->slotInfo,ulTimingInfo,PHY_DELTA_UL+SCHED_DELTA, cell->numSlots);
495
496    ulSchedInfo.cellId = cell->cellId;
497    ulSchedInfo.slotIndInfo.cellId = ulSchedInfo.cellId;
498    ulSchedInfo.slotIndInfo.sfn = ulTimingInfo.sfn;
499    ulSchedInfo.slotIndInfo.slot = ulTimingInfo.slot;
500
501    /* Schedule resources for PRACH */
502    if(cell->firstSib1Transmitted)
503     schPrachResAlloc(cell, &ulSchedInfo, ulTimingInfo);
504
505    schUlSlotInfo = cell->schUlSlotInfo[ulTimingInfo.slot]; 
506    if(schUlSlotInfo->schPuschInfo)
507    {
508       GET_CRNTI(ulSchedInfo.crnti, schUlSlotInfo->puschUe);
509       /* Check the ue drx status if the UE is active for uplink scheduling or not  */
510 #ifdef NR_DRX 
511       ueCb = schGetUeCb(cell, ulSchedInfo.crnti);
512       if(ueCb->ueDrxInfoPres)
513       {
514          if(!ueCb->drxUeCb.drxUlUeActiveStatus)
515             return RFAILED;
516       }
517 #endif
518       ulSchedInfo.dataType |= SCH_DATATYPE_PUSCH;
519       memcpy(&ulSchedInfo.schPuschInfo, schUlSlotInfo->schPuschInfo,
520             sizeof(SchPuschInfo));
521       SCH_FREE(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
522       schUlSlotInfo->schPuschInfo = NULL;
523    }
524
525    if(schUlSlotInfo->pucchPres)
526    {
527       GET_CRNTI(ulSchedInfo.crnti, schUlSlotInfo->pucchUe);
528       ret = fillPucchResourceInfo(schUlSlotInfo->pucchUe, &schUlSlotInfo->schPucchInfo, schInst, ulTimingInfo);
529       if (ret == ROK)
530       {
531          ulSchedInfo.dataType |= SCH_DATATYPE_UCI;
532          memcpy(&ulSchedInfo.schPucchInfo, &schUlSlotInfo->schPucchInfo,
533                sizeof(SchPucchInfo));
534       }
535       else
536       {
537          return RFAILED;
538       }
539       memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
540    }
541
542    /* Send msg to MAC */
543    ret = sendUlSchInfoToMac(&ulSchedInfo, schInst);
544    if(ret != ROK)
545    {
546       DU_LOG("\nERROR  -->  SCH : Sending UL Sch info from SCH to MAC failed");
547    }
548
549    /* Update DL PRB Usage for all stats group which requested for DL Total PRB Usage */
550    node = cmLListFirst(&schCb[schInst].statistics.activeKpiList.ulTotPrbUseList);
551    while(node)
552    {
553       ulTotalPrbUsage = (TotalPrbUsage *)node->node;
554       ulTotalPrbUsage->numPrbUsedForTx += schUlSlotInfo->prbAlloc.numPrbAlloc;
555       ulTotalPrbUsage->totalPrbAvailForTx += MAX_NUM_RB;
556       node = node->next;
557    }
558
559    /* Re-initialize UL Slot */
560    schInitUlSlot(schUlSlotInfo);
561    return ret;
562 }
563
564 /*******************************************************************
565  *
566  * @brief Fills pdcch and pdsch info for msg4
567  *
568  * @details
569  *
570  *    Function : schDlRsrcAllocMsg4
571  *
572  *    Functionality:
573  *       Fills pdcch and pdsch info for msg4
574  *
575  * @params[in] SchCellCb *cell, SlotTimingInfo msg4Time
576  * @params[in] uint8_t ueId, DlMsgSchInfo *dlMsgAlloc
577  * @params[in] uint8_t pdschStartSymbol, uint8_t pdschNumSymbols
578  * @params[in] bool isRetx, SchDlHqProcCb *hqP
579  * @return ROK     - success
580  *         RFAILED - failure
581  *
582  * ****************************************************************/
583 uint8_t schDlRsrcAllocMsg4(SchCellCb *cell, SlotTimingInfo msg4Time, uint8_t ueId, DlMsgSchInfo *dlMsgAlloc,\
584                            uint8_t pdschStartSymbol, uint8_t pdschNumSymbols, bool isRetx, SchDlHqProcCb *hqP)
585 {
586    uint8_t coreset0Idx = 0;
587    uint8_t firstSymbol = 0;
588    uint8_t numSymbols = 0;
589    uint8_t mcs = DEFAULT_MCS;                         /* MCS fixed to 4 */
590    uint8_t dmrsStartSymbol = 0, startSymbol = 0, numSymbol = 0;
591    uint16_t tbSize = 0;
592    uint16_t numRbs;
593    SchBwpDlCfg *initialBwp = NULLP;
594    PdcchCfg *pdcch = NULLP;
595    PdschCfg *pdsch = NULLP;
596    BwpCfg *bwp = NULLP;
597    DlMsgSchInfo *msg4Alloc = NULLP;
598
599    if(cell == NULL)
600    {
601       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() : Cell is NULL");
602       return RFAILED;
603    }
604
605    if(dlMsgAlloc == NULL)
606    {
607       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() :  dlMsgAlloc is NULL");
608       return RFAILED;
609    }
610
611    msg4Alloc = dlMsgAlloc;
612    initialBwp   = &cell->cellCfg.dlCfgCommon.schInitialDlBwp;
613    SCH_ALLOC(msg4Alloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
614
615    if(!msg4Alloc->dlMsgPdcchCfg)
616    {
617       DU_LOG("\nERROR  --> SCH : Memory allocation failed in %s",__func__);
618       return RFAILED;
619    }
620    pdcch = msg4Alloc->dlMsgPdcchCfg;
621    bwp = &msg4Alloc->bwp;
622    coreset0Idx  = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
623
624    fillDlMsgInfo(msg4Alloc, cell->raCb[ueId-1].tcrnti, isRetx, hqP);
625    msg4Alloc->dlMsgPduLen = cell->raCb[ueId-1].dlMsgPduLen;
626
627    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
628    numRbs     = coresetIdxTable[coreset0Idx][1];
629    numSymbols = coresetIdxTable[coreset0Idx][2];
630
631    /* calculate time domain parameters */
632    uint16_t mask = 0x2000;
633    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
634    {
635       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
636          break;
637       else
638          mask = mask>>1;
639    }
640
641    /* fill BWP */
642    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
643    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
644    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
645    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
646
647    /* fill the PDCCH PDU */
648    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
649    pdcch->coresetCfg.durationSymbols = numSymbols;
650    memcpy(pdcch->coresetCfg.freqDomainResource, \
651       cell->cellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
652
653    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
654    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
655    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
656    pdcch->coresetCfg.coreSetType = 0;
657    pdcch->coresetCfg.coreSetSize = numRbs;
658    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
659    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
660    pdcch->numDlDci = 1;
661    pdcch->dci.rnti = cell->raCb[ueId-1].tcrnti;
662    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
663    pdcch->dci.scramblingRnti = 0;
664    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
665    pdcch->dci.aggregLevel = 4;
666    pdcch->dci.beamPdcchInfo.numPrgs = 1;
667    pdcch->dci.beamPdcchInfo.prgSize = 1;
668    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
669    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
670    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
671    pdcch->dci.txPdcchPower.beta_pdcch_1_0 = 0;
672    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
673    pdsch = &pdcch->dci.pdschCfg; 
674    
675    /* fill the PDSCH PDU */
676    uint8_t cwCount = 0;
677    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
678    pdsch->rnti = cell->raCb[ueId-1].tcrnti;
679    pdsch->pduIndex = 0;
680    pdsch->numCodewords = 1;
681    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
682    {
683       pdsch->codeword[cwCount].targetCodeRate = 308;
684       pdsch->codeword[cwCount].qamModOrder = 2;
685       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
686       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
687       if(isRetx != TRUE)
688       {
689          tbSize = schCalcTbSize(msg4Alloc->dlMsgPduLen + TX_PAYLOAD_HDR_LEN); /* MSG4 size + FAPI header size*/
690          hqP->tbInfo[cwCount].tbSzReq = tbSize;
691          pdsch->codeword[cwCount].rvIndex = 0;
692       }
693       else
694       {
695          pdsch->codeword[cwCount].rvIndex = (pdsch->codeword[cwCount].rvIndex +1) & 0x03;
696          tbSize = hqP->tbInfo[cwCount].tbSzReq;
697       }
698       pdsch->codeword[cwCount].tbSize = tbSize;
699    }
700    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
701    pdsch->numLayers = 1;
702    pdsch->transmissionScheme = 0;
703    pdsch->refPoint = 0;
704    pdsch->dmrs.dlDmrsSymbPos = DL_DMRS_SYMBOL_POS; 
705    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
706    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
707    pdsch->dmrs.scid = 0;
708    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
709    pdsch->dmrs.dmrsPorts = 0;
710    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
711    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
712    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
713
714    pdsch->pdschTimeAlloc.startSymb = pdschStartSymbol; 
715    pdsch->pdschTimeAlloc.numSymb = pdschNumSymbols;
716
717    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
718    pdsch->pdschFreqAlloc.startPrb = MAX_NUM_RB;
719    pdsch->pdschFreqAlloc.numPrb = schCalcNumPrb(tbSize, mcs, pdschNumSymbols);
720    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
721
722    /* Find total symbols occupied including DMRS */
723    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
724    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
725     * in that case only PDSCH symbols are marked as occupied */
726    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
727    {
728       startSymbol = pdsch->pdschTimeAlloc.startSymb;
729       numSymbol = pdsch->pdschTimeAlloc.numSymb;
730    }
731    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
732    else
733    {
734       startSymbol = dmrsStartSymbol;
735       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
736    }
737
738    /* Allocate the number of PRBs required for RAR PDSCH */
739    if((allocatePrbDl(cell, msg4Time, startSymbol, numSymbol,\
740       &pdsch->pdschFreqAlloc.startPrb, pdsch->pdschFreqAlloc.numPrb)) != ROK)
741    {
742       DU_LOG("\nERROR  --> SCH : Resource allocation failed for MSG4");
743       SCH_FREE(msg4Alloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
744       return RFAILED;
745    }
746
747    pdsch->beamPdschInfo.numPrgs = 1;
748    pdsch->beamPdschInfo.prgSize = 1;
749    pdsch->beamPdschInfo.digBfInterfaces = 0;
750    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
751    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
752    pdsch->txPdschPower.powerControlOffset = 0;
753    pdsch->txPdschPower.powerControlOffsetSS = 0;
754
755    return ROK;
756 }
757  
758 /*******************************************************************
759  *
760  * @brief Scheduling for Pucch Resource
761  *
762  * @details
763  *
764  *    Function : schAllocPucchResource
765  *
766  *    Functionality:
767  *       Scheduling for Pucch Resource
768  *
769  * @params[in] SchCellCb *cell, SlotTimingInfo pucchTime, crnti
770  * @params[in] SchUeCb *ueCb, bool isRetx, SchDlHqProcCb *hqP
771  * @return ROK     - success
772  *         RFAILED - failure
773  *
774  *******************************************************************/
775
776 uint16_t schAllocPucchResource(SchCellCb *cell, SlotTimingInfo pucchTime, uint16_t crnti,
777                                SchUeCb *ueCb, bool isRetx, SchDlHqProcCb *hqP)
778 {
779    uint16_t pucchSlot = 0;
780    SchUlSlotInfo  *schUlSlotInfo = NULLP;
781
782    pucchSlot = pucchTime.slot;
783    schUlSlotInfo = cell->schUlSlotInfo[pucchSlot];
784    memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
785
786    schUlSlotInfo->pucchPres = true;
787    if(ueCb != NULLP)
788    {
789       /* set HARQ flag to true */
790       schUlSlotInfo->schPucchInfo.harqInfo.harqBitLength = 1; /* 1 bit for HARQ */
791       ADD_DELTA_TO_TIME(pucchTime, pucchTime, 3, cell->numSlots); /* SLOT_DELAY=3 */
792       cmLListAdd2Tail(&(ueCb->hqDlmap[pucchTime.slot]->hqList), &hqP->ulSlotLnk);
793    }
794    return ROK;
795 }
796
797 /*******************************************************************
798  *
799  * @brief Fills pdcch and pdsch info for dedicated DL msg
800  *
801  * @details
802  *
803  *    Function : schDlRsrcAllocDlMsg
804  *
805  *    Functionality:
806  *       Fills pdcch and pdsch info for dl msg
807  *
808  * @params[in] SchCellCb *cell, SlotTimingInfo slotTime
809  * @params[in] uint16_t crnti, uint32_t tbSize
810  * @params[in] DlMsgSchInfo *dlMsgAlloc, uint16_t startPRB
811  * @params[in] uint8_t pdschStartSymbol, uint8_t pdschNumSymbols
812  * @params[in] bool isRetx, SchDlHqProcCb *hqP
813  * @return ROK     - success
814  *         RFAILED - failure
815  *
816  * ****************************************************************/
817 uint8_t schDlRsrcAllocDlMsg(SchCellCb *cell, SlotTimingInfo slotTime, uint16_t crnti,
818                 uint32_t tbSize, DlMsgSchInfo *dlMsgAlloc, uint16_t startPRB, uint8_t pdschStartSymbol,
819                 uint8_t pdschNumSymbols, bool isRetx, SchDlHqProcCb *hqP)
820 {
821    uint8_t ueId=0;
822    uint8_t cwCount = 0;
823    PdcchCfg *pdcch = NULLP;
824    PdschCfg *pdsch = NULLP;
825    BwpCfg *bwp = NULLP;
826    SchUeCb ueCb;
827    SchControlRsrcSet coreset1;
828    SchPdschConfig pdschCfg;
829    uint8_t dmrsStartSymbol, startSymbol, numSymbol;
830
831    SCH_ALLOC(dlMsgAlloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
832    if(!dlMsgAlloc->dlMsgPdcchCfg)
833    {
834       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schDlRsrcAllocDlMsg");
835       return RFAILED;
836    }
837    pdcch = dlMsgAlloc->dlMsgPdcchCfg;
838    bwp = &dlMsgAlloc->bwp;
839
840    GET_UE_ID(crnti, ueId);
841    ueCb  = cell->ueCb[ueId-1];
842    coreset1 = ueCb.ueCfg.spCellCfg.servCellRecfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
843    pdschCfg = ueCb.ueCfg.spCellCfg.servCellRecfg.initDlBwp.pdschCfg;
844
845    /* fill BWP */
846    bwp->freqAlloc.numPrb = MAX_NUM_RB;
847    bwp->freqAlloc.startPrb = 0;
848    bwp->subcarrierSpacing = cell->sib1SchCfg.bwp.subcarrierSpacing;
849    bwp->cyclicPrefix = cell->sib1SchCfg.bwp.cyclicPrefix;
850
851    /* fill the PDCCH PDU */
852    //Considering coreset1 also starts from same symbol as coreset0
853    pdcch->coresetCfg.startSymbolIndex = coresetIdxTable[0][3];
854    pdcch->coresetCfg.durationSymbols = coreset1.duration;
855    memcpy(pdcch->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
856    pdcch->coresetCfg.cceRegMappingType = coreset1.cceRegMappingType; /* non-interleaved */
857    pdcch->coresetCfg.regBundleSize = 6;   /* must be 6 for non-interleaved */
858    pdcch->coresetCfg.interleaverSize = 0; /* NA for non-interleaved */
859    pdcch->coresetCfg.coreSetType = 1; /* non PBCH coreset */
860    //Considering number of RBs in coreset1 is same as coreset0
861    pdcch->coresetCfg.coreSetSize = coresetIdxTable[0][1];
862    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
863    pdcch->coresetCfg.precoderGranularity =  coreset1.precoderGranularity;
864    pdcch->numDlDci = 1;
865    pdcch->dci.rnti = ueCb.crnti;
866    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
867    pdcch->dci.scramblingRnti = 0;
868    pdcch->dci.cceIndex = 0; /* 0-3 for UL and 4-7 for DL */
869    pdcch->dci.aggregLevel = 4;
870    pdcch->dci.beamPdcchInfo.numPrgs = 1;
871    pdcch->dci.beamPdcchInfo.prgSize = 1;
872    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
873    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
874    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
875    pdcch->dci.txPdcchPower.beta_pdcch_1_0 = 0;
876    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
877
878    pdsch = &pdcch->dci.pdschCfg;
879    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
880    pdsch->rnti = ueCb.crnti;
881    pdsch->pduIndex = 0;
882    pdsch->numCodewords = 1;
883    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
884    {
885       pdsch->codeword[cwCount].targetCodeRate = 308;
886       pdsch->codeword[cwCount].qamModOrder = ueCb.ueCfg.dlModInfo.modOrder;
887       pdsch->codeword[cwCount].mcsIndex = ueCb.ueCfg.dlModInfo.mcsIndex;
888       pdsch->codeword[cwCount].mcsTable = ueCb.ueCfg.dlModInfo.mcsTable;
889       pdsch->codeword[cwCount].rvIndex = 0;
890
891       if (isRetx != TRUE)
892       {
893          tbSize +=TX_PAYLOAD_HDR_LEN;
894          hqP->tbInfo[cwCount].tbSzReq = tbSize;
895       }
896       pdsch->codeword[cwCount].tbSize = tbSize;
897    }
898    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
899    pdsch->numLayers = 1;
900    pdsch->transmissionScheme = 0;
901    pdsch->refPoint = 0;
902    pdsch->dmrs.dlDmrsSymbPos = DL_DMRS_SYMBOL_POS; 
903    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
904    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
905    pdsch->dmrs.scid = 0;
906    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
907    pdsch->dmrs.dmrsPorts = 0;
908    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
909    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
910    pdsch->dmrs.dmrsAddPos       = pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos;
911
912    pdsch->pdschTimeAlloc.startSymb = pdschStartSymbol; 
913    pdsch->pdschTimeAlloc.numSymb = pdschNumSymbols;
914
915    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
916    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
917    pdsch->pdschFreqAlloc.startPrb = startPRB; /*Start PRB will be already known*/
918    pdsch->pdschFreqAlloc.numPrb = schCalcNumPrb(tbSize, ueCb.ueCfg.dlModInfo.mcsIndex, pdschNumSymbols);
919
920    /* Find total symbols occupied including DMRS */
921    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
922    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
923     * in that case only PDSCH symbols are marked as occupied */
924    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
925    {
926       startSymbol = pdsch->pdschTimeAlloc.startSymb;
927       numSymbol = pdsch->pdschTimeAlloc.numSymb;
928    }
929    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
930    else
931    {
932       startSymbol = dmrsStartSymbol;
933       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.numSymb;
934    }
935
936    /* Allocate the number of PRBs required for DL PDSCH */
937    if((allocatePrbDl(cell, slotTime, startSymbol, numSymbol,\
938       &pdsch->pdschFreqAlloc.startPrb, pdsch->pdschFreqAlloc.numPrb)) != ROK)
939    {
940       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for DL MSG");
941       SCH_FREE(dlMsgAlloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
942       return RFAILED;
943    }
944
945    pdsch->beamPdschInfo.numPrgs = 1;
946    pdsch->beamPdschInfo.prgSize = 1;
947    pdsch->beamPdschInfo.digBfInterfaces = 0;
948    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
949    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
950    pdsch->txPdschPower.powerControlOffset = 0;
951    pdsch->txPdschPower.powerControlOffsetSS = 0;
952
953    return ROK;
954 }
955
956 /*******************************************************************
957  *
958  * @brief Fills k0 and k1 information table for FDD 
959  *
960  * @details
961  *
962  *    Function : BuildK0K1TableForFdd 
963  *
964  *    Functionality:
965  *      Fills k0 and k1 information table for FDD
966  *
967  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
968  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
969  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
970  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
971  * @return ROK     - success
972  *         RFAILED - failure
973  *
974  * ****************************************************************/
975 void BuildK0K1TableForFdd(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres,SchPdschCfgCmn pdschCmnCfg,\
976 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
977 {
978    
979    uint8_t k1TmpVal =0, cfgIdx=0;
980    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, numTimeDomAlloc=0;
981    
982    /* TODO Commented these below lines for resolving warnings. Presently these variable are not 
983     * required but this will require for harq processing */
984    // uint8_t k0TmpVal = 0; 
985    // SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
986    // SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
987
988    /* Initialization the structure and storing the total slot values. */
989    memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
990    k0K1InfoTbl->tblSize = cell->numSlots;
991    
992    /* Storing time domain resource allocation list based on common or dedicated configuration. */
993    if(pdschCfgCmnPres == true)
994    {
995       numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
996       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
997       {
998          /*TODO uncomment this line during harq processing */
999          //cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
1000       }
1001    }
1002    else
1003    {
1004       numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
1005       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1006       {
1007          /*TODO uncomment this line during harq processing */
1008          //dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
1009       }
1010    }
1011    
1012    /* Checking all the slots for K0 and K1 values. */
1013    for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1014    {
1015       numK0 = 0;
1016       /* Storing the values of k0 based on time domain resource
1017        * allocation list. If the value is unavailable then fill default values,
1018        * As per 38.331 PDSCH-TimeDomainResourceAllocation field descriptions. */
1019       for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX));  k0Index++)
1020       {
1021          /* TODO These if 0 we will remove during harq processing */
1022 #if 0
1023          if(pdschCfgCmnPres == true)
1024          {
1025             k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
1026          }
1027          else
1028          {
1029             if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
1030             {
1031                k0TmpVal = *(dedTimeDomRsrcAllocList[k0Index].k0);
1032             }
1033             else
1034             { 
1035                k0TmpVal = DEFAULT_K0_VALUE;
1036             }
1037          }
1038 #endif         
1039          /* Checking all the Ul Alloc values. If value is less than MIN_NUM_K1_IDX
1040           * then skip else continue storing the values. */
1041          numK1 = 0;
1042          for(k1Index = 0; k1Index < ulAckListCount; k1Index++)
1043          {
1044             k1TmpVal = UlAckTbl[k1Index];
1045             if(k1TmpVal <= MIN_NUM_K1_IDX)
1046             {
1047                continue;
1048             }
1049
1050             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.k1Indexes[numK1++] = k1Index;
1051             /* TODO Store K1 index where harq feedback will be received in harq table. */ 
1052          }
1053          if(numK1)
1054          {
1055             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.numK1 = numK1;
1056             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k0Index = k0Index;
1057             numK0++;
1058          }
1059       }
1060       if(numK0)
1061       {
1062          k0K1InfoTbl->k0k1TimingInfo[slotIdx].numK0 = numK0;
1063       }
1064    }
1065 }
1066
1067 /*******************************************************************
1068  *
1069  * @brief Fills k0 and k1 information table  
1070  *
1071  * @details
1072  *
1073  *    Function : BuildK0K1Table
1074  *
1075  *    Functionality:
1076  *       Fills K0 and k1 information table 
1077  *
1078  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
1079  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
1080  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
1081  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
1082  * @return ROK     - success
1083  *         RFAILED - failure
1084  *
1085  * ****************************************************************/
1086 void BuildK0K1Table(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres, SchPdschCfgCmn pdschCmnCfg,\
1087 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
1088 {
1089
1090 #ifdef NR_TDD
1091    SlotConfig  slotCfg;
1092    bool ulSlotPresent = false;
1093    uint8_t k0TmpVal = 0, k1TmpVal =0, tmpSlot=0, startSymbol=0, endSymbol=0, checkSymbol=0;
1094    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, cfgIdx=0, numTimeDomAlloc =0, totalCfgSlot =0;
1095    SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
1096    SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
1097 #endif
1098
1099    if(cell->cellCfg.dupMode == DUPLEX_MODE_FDD)
1100    {
1101       BuildK0K1TableForFdd(cell, k0K1InfoTbl, pdschCfgCmnPres, pdschCmnCfg, pdschDedCfg, ulAckListCount, UlAckTbl);
1102    }
1103    else
1104    {
1105 #ifdef NR_TDD
1106       
1107       /* Initialization the K0K1 structure, total num of slot and calculating the slot pattern length. */
1108       memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
1109       k0K1InfoTbl->tblSize = cell->numSlots;
1110       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbScs, cell->cellCfg.tddCfg.tddPeriod);
1111       
1112       /* Storing time domain resource allocation list based on common or 
1113        * dedicated configuration availability. */
1114       if(pdschCfgCmnPres == true)
1115       {
1116          numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
1117          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1118          {
1119             cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
1120          }
1121       }
1122       else
1123       {
1124          numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
1125          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1126          {
1127             dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
1128          }
1129       }
1130
1131       /* Checking all possible indexes for K0 and K1 values. */
1132       for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1133       {
1134          /* If current slot is UL or FLEXI then Skip because PDCCH is sent only in DL slots. */
1135          slotCfg = schGetSlotSymbFrmt(slotIdx%totalCfgSlot, cell->slotFrmtBitMap);
1136          if(slotCfg == UL_SLOT || slotCfg == FLEXI_SLOT)
1137          {
1138             continue;
1139          }
1140         
1141          ulSlotPresent = false;
1142          /* Storing K0 , start symbol and length symbol for further processing.
1143           * If K0 value is not available then we can fill the default values
1144           * given in spec 38.331. */
1145          numK0 = 0;
1146          for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX)); k0Index++)
1147          {
1148             if(pdschCfgCmnPres == true)
1149             {
1150                k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
1151                startSymbol = cmnTimeDomRsrcAllocList[k0Index].startSymbol;
1152                endSymbol = startSymbol + cmnTimeDomRsrcAllocList[k0Index].lengthSymbol;
1153             }
1154             else
1155             {
1156                if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
1157                {
1158                   k0TmpVal =  *(dedTimeDomRsrcAllocList[k0Index].k0);
1159                }
1160                else
1161                {
1162                   k0TmpVal = DEFAULT_K0_VALUE;
1163                }
1164                startSymbol = dedTimeDomRsrcAllocList[k0Index].startSymbol;
1165                endSymbol = startSymbol + dedTimeDomRsrcAllocList[k0Index].symbolLength;
1166             }
1167             
1168             /* If current slot + k0 is UL then skip the slot
1169              * else if it is DL slot then continue the next steps
1170              * else if it is a FLEXI slot then check symbols of slot, It should not
1171              * contain any UL slot. */
1172             tmpSlot = (slotIdx+k0TmpVal) % totalCfgSlot;
1173             slotCfg = schGetSlotSymbFrmt(tmpSlot, cell->slotFrmtBitMap);
1174             if(slotCfg == UL_SLOT)
1175             {
1176                continue;
1177             }
1178             if(slotCfg == FLEXI_SLOT)
1179             {
1180                for(checkSymbol = startSymbol; checkSymbol<endSymbol; checkSymbol ++)
1181                {
1182                   slotCfg = cell->slotCfg[tmpSlot][checkSymbol];
1183                   if(slotCfg == UL_SLOT)
1184                   {
1185                      ulSlotPresent = true;
1186                      break;
1187                   }
1188                }
1189                if(ulSlotPresent == true)
1190                {
1191                   continue;
1192                }
1193             }
1194
1195              ulSlotPresent = false; //Re-initializing
1196
1197             /* If current slot + k0 + k1 is a DL slot then skip the slot
1198              * else if it is UL slot then store the information 
1199              * else if it is FLEXI slot then check the symbols, it must have
1200              * at least one UL symbol. */
1201             numK1 = 0;
1202             for(k1Index = 0; k1Index < ulAckListCount; k1Index++)
1203             {
1204                k1TmpVal = UlAckTbl[k1Index];
1205                if(k1TmpVal > MIN_NUM_K1_IDX)
1206                {
1207                   tmpSlot = (slotIdx+k0TmpVal+k1TmpVal) % totalCfgSlot;
1208                   slotCfg =  schGetSlotSymbFrmt(tmpSlot, cell->slotFrmtBitMap);
1209                   if(slotCfg == DL_SLOT) 
1210                   {
1211                      continue;
1212                   }   
1213                   if(slotCfg == FLEXI_SLOT)
1214                   {
1215                      for(checkSymbol = 0; checkSymbol< MAX_SYMB_PER_SLOT;checkSymbol++)
1216                      {
1217                         if(cell->slotCfg[tmpSlot][checkSymbol] == UL_SYMBOL)
1218                         {
1219                            ulSlotPresent = true;
1220                            break;
1221                         }
1222                      }
1223                   }
1224                   if(ulSlotPresent == true || slotCfg ==  UL_SLOT)
1225                   {
1226                      k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.k1Indexes[numK1++] = k1Index;
1227                      /* TODO Store K1 index where harq feedback will be received
1228                       * in harq table. */
1229                   }
1230                }
1231             }
1232             
1233             /* Store all the values if all condition satisfies. */
1234             if(numK1)
1235             {
1236                k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.numK1 = numK1;
1237                k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k0Index = k0Index;
1238                numK0++;
1239             }
1240          }
1241          if(numK0)
1242          {
1243             k0K1InfoTbl->k0k1TimingInfo[slotIdx].numK0 = numK0;
1244          }
1245       }
1246 #endif
1247    }
1248 }
1249
1250 /*******************************************************************
1251 *
1252 * @brief Fills K2 information table for FDD
1253 *
1254 * @details
1255 *
1256 *    Function : BuildK2InfoTableForFdd 
1257 *
1258 *    Functionality:
1259 *       Fills K2 information table for FDD
1260 *
1261 * @params[in] SchCellCb *cell,SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[],
1262 * uint16_t puschSymTblSize,SchK2TimingInfoTbl *k2InfoTbl
1263 * @return ROK     - success
1264 *         RFAILED - failure
1265 *
1266 * ****************************************************************/
1267 void BuildK2InfoTableForFdd(SchCellCb *cell, SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[], uint16_t puschSymTblSize,\
1268 SchK2TimingInfoTbl *msg3K2InfoTbl, SchK2TimingInfoTbl *k2InfoTbl)
1269 {
1270    uint16_t slotIdx=0, k2Index=0, k2TmpIdx=0, msg3K2TmpIdx=0;
1271
1272    /* Initialization the structure and storing the total slot values. */
1273    memset(k2InfoTbl, 0, sizeof(SchK2TimingInfoTbl));
1274    k2InfoTbl->tblSize = cell->numSlots;
1275    if(msg3K2InfoTbl)
1276       msg3K2InfoTbl->tblSize = cell->numSlots;
1277    
1278    /* Checking all possible indexes for K2. */
1279    for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1280    {
1281       /* Storing K2 values. */
1282       for(k2Index = 0; ((k2Index < puschSymTblSize) && (k2Index < MAX_NUM_K2_IDX)); k2Index++)
1283       {
1284          k2TmpIdx= k2InfoTbl->k2TimingInfo[slotIdx].numK2;
1285          k2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[k2TmpIdx] = k2Index;
1286          k2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1287
1288          /* Updating K2 values for MSG3 */
1289          if(msg3K2InfoTbl)
1290          {
1291             msg3K2TmpIdx = msg3K2InfoTbl->k2TimingInfo[slotIdx].numK2;
1292             msg3K2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[msg3K2TmpIdx] = k2Index;
1293             msg3K2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1294          }
1295       }
1296    }
1297 }
1298
1299 /*******************************************************************
1300  *
1301  * @brief Fills K2 information table
1302  *
1303  * @details
1304  *
1305  *    Function : BuildK2InfoTable 
1306  *
1307  *    Functionality:
1308  *       Fills K2 information table
1309  *
1310  * @params[in] SchCellCb *cell,SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[],
1311  * uint16_t puschSymTblSize, SchK2TimingInfoTbl *k2InfoTbl
1312  * @return ROK     - success
1313  *         RFAILED - failure
1314  *
1315  * ****************************************************************/
1316 void BuildK2InfoTable(SchCellCb *cell, SchPuschTimeDomRsrcAlloc timeDomRsrcAllocList[], uint16_t puschSymTblSize,\
1317 SchK2TimingInfoTbl *msg3K2InfoTbl, SchK2TimingInfoTbl *k2InfoTbl)
1318 {
1319
1320 #ifdef NR_TDD
1321    bool dlSymbolPresent = false;
1322    uint8_t slotIdx=0, k2Index=0, k2Val=0, k2TmpVal=0, msg3K2TmpVal=0, msg3Delta=0, numK2 =0, currentSymbol =0;
1323    uint8_t startSymbol =0, endSymbol =0, checkSymbol=0, totalCfgSlot=0, slotCfg=0;
1324    SlotConfig currentSlot;
1325 #endif
1326
1327    if(cell->cellCfg.dupMode == DUPLEX_MODE_FDD)
1328    {
1329       BuildK2InfoTableForFdd(cell, timeDomRsrcAllocList, puschSymTblSize, msg3K2InfoTbl, k2InfoTbl);
1330    }
1331    else
1332    {
1333 #ifdef NR_TDD
1334
1335       /* Initialization the structure and storing the total slot values. */
1336       memset(k2InfoTbl, 0, sizeof(SchK2TimingInfoTbl));
1337       k2InfoTbl->tblSize = cell->numSlots;
1338       if(msg3K2InfoTbl)
1339          msg3K2InfoTbl->tblSize = cell->numSlots;
1340       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbScs, cell->cellCfg.tddCfg.tddPeriod);
1341
1342       /* Checking all possible indexes for K2. */
1343       for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1344       {
1345          currentSlot = schGetSlotSymbFrmt(slotIdx % totalCfgSlot, cell->slotFrmtBitMap);
1346          
1347          /* If current slot is UL then skip because PDCCH is sent only in DL slots */
1348          if(currentSlot != UL_SLOT)
1349          {
1350             for(k2Index = 0; ((k2Index < puschSymTblSize) && (k2Index < MAX_NUM_K2_IDX)); k2Index++)
1351             {
1352                /* Storing k2, startSymbol, endSymbol information for further processing.
1353                 * If k2 is absent then fill the default values given in spec 38.331
1354                 * PUSCH-TimeDomainResourceAllocationList field descriptions */
1355                k2Val = timeDomRsrcAllocList[k2Index].k2;
1356                if(!k2Val)
1357                {
1358                   switch(cell->cellCfg.ssbScs)
1359                   {
1360                      case SCS_15KHZ:
1361                         k2Val = DEFAULT_K2_VALUE_FOR_SCS15;
1362                         break;
1363                      case SCS_30KHZ:
1364                         k2Val = DEFAULT_K2_VALUE_FOR_SCS30;
1365                         break;
1366                      case SCS_60KHZ:
1367                         k2Val = DEFAULT_K2_VALUE_FOR_SCS60;
1368                         break;
1369                      case SCS_120KHZ:
1370                         k2Val = DEFAULT_K2_VALUE_FOR_SCS120;
1371                         break;
1372                   }
1373                }
1374                
1375                /* Current slot + k2 should be either UL or FLEXI slot.
1376                 * If slot is FLEXI then check all the symbols of that slot,
1377                 * it should not contain any DL or FLEXI slot */
1378                k2TmpVal = (slotIdx + k2Val) % totalCfgSlot;
1379                slotCfg = schGetSlotSymbFrmt(k2TmpVal, cell->slotFrmtBitMap);
1380                if(slotCfg != DL_SLOT)
1381                {
1382                   if(slotCfg == FLEXI_SLOT)
1383                   {
1384                      startSymbol =  timeDomRsrcAllocList[k2Index].startSymbol;
1385                      endSymbol   =  startSymbol+ timeDomRsrcAllocList[k2Index].symbolLength;
1386                      dlSymbolPresent = false;
1387                      for(checkSymbol= startSymbol; checkSymbol<endSymbol; checkSymbol++)
1388                      {
1389                         currentSymbol = cell->slotCfg[k2TmpVal][checkSymbol];
1390                         if(currentSymbol == DL_SLOT || currentSymbol == FLEXI_SLOT)
1391                         {
1392                            dlSymbolPresent = true;
1393                            break;
1394                         }
1395                      }
1396                   }
1397                   /* Store all the values if all condition satisfies. */
1398                   if(dlSymbolPresent != true || slotCfg == UL_SLOT)
1399                   {
1400                      numK2 = k2InfoTbl->k2TimingInfo[slotIdx].numK2;
1401                      k2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[numK2] = k2Index;
1402                      k2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1403                   }
1404                }
1405
1406                if(msg3K2InfoTbl)
1407                {
1408                    msg3Delta = puschDeltaTable[cell->numerology];
1409
1410                   /* Check for K2 for MSG3 */
1411                   /* Current slot + k2 should be either UL or FLEXI slot.
1412                    * If slot is FLEXI then check all the symbols of that slot,
1413                    * it should not contain any DL or FLEXI slot */
1414                   msg3K2TmpVal = (slotIdx + k2Val + msg3Delta) % totalCfgSlot;
1415                   slotCfg = schGetSlotSymbFrmt(msg3K2TmpVal, cell->slotFrmtBitMap);
1416                   if(slotCfg != DL_SLOT)
1417                   {
1418                      if(slotCfg == FLEXI_SLOT)
1419                      {
1420                         startSymbol =  timeDomRsrcAllocList[k2Index].startSymbol;
1421                         endSymbol   =  startSymbol+ timeDomRsrcAllocList[k2Index].symbolLength;
1422                         dlSymbolPresent = false;
1423                         for(checkSymbol= startSymbol; checkSymbol<endSymbol; checkSymbol++)
1424                         {
1425                            currentSymbol = cell->slotCfg[msg3K2TmpVal][checkSymbol];
1426                            if(currentSymbol == DL_SLOT || currentSymbol == FLEXI_SLOT)
1427                            {
1428                               dlSymbolPresent = true;
1429                               break;
1430                            }
1431                         }
1432                      }
1433                      /* Store all the values if all condition satisfies. */
1434                      if(dlSymbolPresent != true || slotCfg == UL_SLOT)
1435                      {
1436                         numK2 = msg3K2InfoTbl->k2TimingInfo[slotIdx].numK2;
1437                         msg3K2InfoTbl->k2TimingInfo[slotIdx].k2Indexes[numK2] = k2Index;
1438                         msg3K2InfoTbl->k2TimingInfo[slotIdx].numK2++;
1439                      }
1440                   }
1441                }
1442             }
1443          }
1444       }
1445 #endif
1446    }
1447 }
1448
1449 /*******************************************************************************************
1450  *
1451  * @brief Allocate the PRB using RRM policy
1452  *
1453  * @details
1454  *
1455  *    Function : prbAllocUsingRRMPolicy
1456  *
1457  *    Functionality:
1458  *      [Step1]: Traverse each Node in the LC list
1459  *      [Step2]: Check whether the LC has ZERO requirement then clean this LC
1460  *      [Step3]: Calcualte the maxPRB for this LC.
1461  *              a. For Dedicated LC, maxPRB = sum of remainingReservedPRB and
1462  *              sharedPRB
1463  *              b. For Default, just SharedPRB count
1464  *      [Step4]: If the LC is the First one to be allocated for this UE then add
1465  *      TX_PAYLODN_LEN to reqBO 
1466  *      [Step5]: Calculate the estimate PRB and estimate BO to be allocated
1467  *               based on reqBO and maxPRB left.
1468  *      [Step6]: Based on calculated PRB, Update Reserved PRB and Shared PRB counts
1469  *      [Step7]: Deduce the reqBO based on allocBO and move the LC node to last.
1470  *      [Step8]: Continue the next loop from List->head
1471  *
1472  *      [Loop Exit]:
1473  *        [Exit1]: If all the LCs are allocated in list
1474  *        [Exit2]: If PRBs are exhausted
1475  *
1476  * @params[in] I/P > lcLinkList pointer (LcInfo list)
1477  *             I/P > IsDedicatedPRB (Flag to indicate that RESERVED PRB to use 
1478  *             I/P > mcsIdx and PDSCH symbols count 
1479  *             I/P & O/P > Shared PRB , reserved PRB Count
1480  *             I/P & O/P > Total TBS size accumulated
1481  *             I/P & O/P > isTxPayloadLenAdded[For DL] : Decision flag to add the TX_PAYLOAD_HDR_LEN
1482  *             I/P & O/P > srRcvd Flag[For UL] : Decision flag to add UL_GRANT_SIZE
1483  *
1484  * @return void
1485  *
1486  * *******************************************************************************************/
1487 void prbAllocUsingRRMPolicy(CmLListCp *lcLL, bool isDedicatedPRB, uint16_t mcsIdx,uint8_t numSymbols,\
1488                   uint16_t *sharedPRB, uint16_t *reservedPRB, bool *isTxPayloadLenAdded, bool *srRcvd)
1489 {
1490    CmLList *node = NULLP;
1491    LcInfo *lcNode = NULLP;
1492    uint16_t remReservedPRB = 0, estPrb = 0, maxPRB = 0;
1493
1494    if(lcLL == NULLP)
1495    {
1496       DU_LOG("\nERROR --> SCH: LcList not present");
1497       return;
1498    }
1499    node = lcLL->first;
1500
1501    /*Only for Dedicated LcList, Valid value will be assigned to remReservedPRB
1502     * For Other LcList, remReservedPRB = 0*/
1503    if(reservedPRB != NULLP && isDedicatedPRB == TRUE)
1504    {
1505       remReservedPRB = *reservedPRB;
1506    }
1507
1508    /*[Step1]*/
1509    while(node)
1510    {
1511 #if 0
1512       /*For Debugging purpose*/
1513       printLcLL(lcLL);
1514 #endif
1515       lcNode = (LcInfo *)node->node;
1516
1517       /* [Step2]: Below condition will hit in rare case as it has been taken care during the cleaning 
1518        * process of LCID which was fully allocated. Check is just for safety purpose*/
1519       if(lcNode->reqBO == 0 && lcNode->allocBO == 0)
1520       {
1521          DU_LOG("\nERROR --> SCH: LCID:%d has no requirement, clearing this node",\
1522                lcNode->lcId);
1523          deleteNodeFromLList(lcLL, node);
1524          SCH_FREE(lcNode, sizeof(LcInfo));
1525          node = lcLL->first; 
1526          continue;
1527       }
1528
1529       /*[Exit1]: All LCs are allocated(allocBO = 0 for fully unallocated LC)*/
1530       if(lcNode->allocBO != 0)
1531       {
1532          DU_LOG("\nDEBUG  -->  SCH: All LC are allocated [SharedPRB:%d]",*sharedPRB);
1533          return;
1534       }
1535
1536       /*[Exit2]: If PRBs are exhausted*/
1537       if(isDedicatedPRB)
1538       {
1539          /*Loop Exit: All resources exhausted*/
1540          if(remReservedPRB == 0 && *sharedPRB == 0)
1541          {
1542             DU_LOG("\nDEBUG  -->  SCH: Dedicated resources exhausted for LC:%d",lcNode->lcId);
1543             return;
1544          }
1545       }
1546       else
1547       {
1548          /*Loop Exit: All resources exhausted*/
1549          if(*sharedPRB == 0)
1550          {
1551             DU_LOG("\nDEBUG  -->  SCH: Default resources exhausted for LC:%d",lcNode->lcId);
1552             return;
1553          }
1554       }
1555
1556       /*[Step3]*/
1557       maxPRB = remReservedPRB + *sharedPRB;
1558
1559       /*[Step4]*/
1560       if((isTxPayloadLenAdded != NULLP) && (*isTxPayloadLenAdded == FALSE))
1561       {
1562          DU_LOG("\nDEBUG  -->  SCH: LC:%d is the First node to be allocated which includes TX_PAYLOAD_HDR_LEN",\
1563                lcNode->lcId);
1564          *isTxPayloadLenAdded = TRUE;
1565          lcNode->allocBO = calculateEstimateTBSize((lcNode->reqBO + TX_PAYLOAD_HDR_LEN),\
1566                mcsIdx, numSymbols, maxPRB, &estPrb);
1567          lcNode->allocBO -=TX_PAYLOAD_HDR_LEN;
1568       }
1569       else if((srRcvd != NULLP) && (*srRcvd == TRUE))
1570       {
1571          DU_LOG("\nDEBUG  --> SCH: LC:%d is the First node to be allocated which includes UL_GRANT_SIZE",\
1572                lcNode->lcId);
1573          *srRcvd = FALSE;
1574          lcNode->reqBO += UL_GRANT_SIZE;
1575          lcNode->allocBO = calculateEstimateTBSize(lcNode->reqBO, mcsIdx, numSymbols, maxPRB, &estPrb);
1576       }
1577       else
1578       {
1579          /*[Step4]*/
1580          lcNode->allocBO = calculateEstimateTBSize(lcNode->reqBO,\
1581                mcsIdx, numSymbols, maxPRB, &estPrb);
1582       }
1583
1584       /*[Step6]:Re-adjust the reservedPRB pool count and *SharedPRB Count based on
1585        * estimated PRB allocated*/
1586       if((isDedicatedPRB == TRUE) && (estPrb <= remReservedPRB))
1587       {
1588          remReservedPRB = remReservedPRB - estPrb;
1589       }
1590       else   /*LC requirement need PRB share from SharedPRB*/
1591       {
1592          if(*sharedPRB <=  (estPrb - remReservedPRB))
1593          {
1594             DU_LOG("\nDEBUG  -->  SCH: SharedPRB is less");
1595             *sharedPRB = 0;
1596          }
1597          else
1598          {
1599             *sharedPRB = *sharedPRB - (estPrb - remReservedPRB);
1600          }
1601          remReservedPRB = 0;
1602       }
1603
1604       /*[Step7]*/
1605       lcNode->reqBO -= lcNode->allocBO;  /*Update the reqBO with remaining bytes unallocated*/
1606       lcNode->allocPRB = estPrb;
1607       cmLListAdd2Tail(lcLL, cmLListDelFrm(lcLL, node));
1608
1609       /*[Step8]:Next loop: First LC to be picked from the list
1610        * because Allocated Nodes are moved to the last*/
1611       node = lcLL->first; 
1612
1613    }
1614    return;
1615 }
1616
1617 /*******************************************************************************************
1618  *
1619  * @brief Check the LC List and fill the LC and GrantSize to be sent to MAC as
1620  * BO Report
1621  *
1622  * @details
1623  *
1624  *    Function : updateGrantSizeForBoRpt
1625  *
1626  *    Functionality:
1627  *             Check the LC List and fill the LC and GrantSize to be sent to MAC as
1628  *             BO Report in dlMsgAlloc Pointer
1629  *
1630  * @params[in] I/P > lcLinkList pointer (LcInfo list)
1631  *             I/P & O/P > dlMsgAlloc[for DL](Pending LC to be added in this context) 
1632  *             I/P & O/P > BsrInfo (applicable for UL)
1633  *             I/P & O/P > accumalatedBOSize
1634  * @return void
1635  *
1636  * *******************************************************************************************/
1637 void updateGrantSizeForBoRpt(CmLListCp *lcLL, DlMsgSchInfo *dlMsgAlloc,\
1638                                 BsrInfo *bsrInfo, uint32_t *accumalatedBOSize)
1639 {
1640    CmLList *node = NULLP, *next = NULLP;
1641    LcInfo *lcNode = NULLP;
1642
1643    if(lcLL == NULLP)
1644    {
1645       DU_LOG("\nERROR --> SCH: LcList not present");
1646       return;
1647    }
1648
1649    if(lcLL->count)
1650    {
1651       node = lcLL->first;
1652    }
1653    else
1654    {
1655       /*lcLL is empty*/
1656       return;
1657    }
1658
1659    /*Traverse List*/
1660    while(node)
1661    {
1662       next = node->next;
1663       lcNode = (LcInfo *)node->node;
1664       if(lcNode != NULLP)
1665       {
1666          DU_LOG("\nINFO   -->  SCH : LcID:%d, [reqBO, allocBO, allocPRB]:[%d,%d,%d]",\
1667                lcNode->lcId, lcNode->reqBO, lcNode->allocBO, lcNode->allocPRB);
1668          if(dlMsgAlloc != NULLP)
1669          {
1670
1671             /*Add this LC to dlMsgAlloc so that if this LC gets allocated, BO
1672              * report for allocation can be sent to MAC*/
1673             dlMsgAlloc->numOfTbs = 1;
1674             dlMsgAlloc->transportBlock[0].lcSchInfo[dlMsgAlloc->transportBlock[0].numLc].lcId = lcNode->lcId;
1675             dlMsgAlloc->transportBlock[0].lcSchInfo[dlMsgAlloc->transportBlock[0].numLc].schBytes = lcNode->allocBO;
1676
1677             /*Calculate the Total Payload/BO size allocated*/
1678             *accumalatedBOSize += dlMsgAlloc->transportBlock[0].lcSchInfo[dlMsgAlloc->transportBlock[0].numLc].schBytes; 
1679
1680             DU_LOG("\nINFO   -->  SCH: Added in MAC BO report: LCID:%d,reqBO:%d,Idx:%d, TotalBO Size:%d",\
1681                   lcNode->lcId,lcNode->reqBO, dlMsgAlloc->transportBlock[0].numLc, *accumalatedBOSize);
1682
1683             dlMsgAlloc->transportBlock[0].numLc++;
1684             handleLcLList(lcLL, lcNode->lcId, DELETE);
1685          }
1686          else if(bsrInfo != NULLP)
1687          {
1688             *accumalatedBOSize += lcNode->allocBO;   
1689             DU_LOG("\nINFO   --> SCH: UL : LCID:%d,reqBO:%d, TotalBO Size:%d",\
1690                   lcNode->lcId,lcNode->reqBO, *accumalatedBOSize);
1691          }
1692       }
1693       node = next;
1694    }/*End of while*/
1695    return;
1696 }
1697
1698 /*******************************************************************
1699 *
1700 * @brief fill DL message information for MSG4 and Dedicated DL Msg
1701 *
1702 * @details
1703 *
1704 *    Function : fillDlMsgInfo
1705 *
1706 *    Functionality:
1707 *       fill DL message information for MSG4 and Dedicated DL Msg
1708 *
1709 * @params[in] DlMsgInfo *dlMsgInfo,  uint16_t crnti
1710 * @params[in] bool isRetx, SchDlHqProcCb *hqP
1711 * @return void
1712 *
1713 *******************************************************************/
1714 void fillDlMsgInfo(DlMsgSchInfo *dlMsgSchInfo, uint16_t crnti, bool isRetx, SchDlHqProcCb *hqP)
1715 {
1716    hqP->tbInfo[0].isEnabled = TRUE;
1717    hqP->tbInfo[0].state = HQ_TB_WAITING;
1718    hqP->tbInfo[0].txCntr++;
1719    hqP->tbInfo[1].isEnabled = TRUE;
1720    hqP->tbInfo[1].state = HQ_TB_WAITING;
1721    hqP->tbInfo[1].txCntr++;
1722    dlMsgSchInfo->crnti = crnti;
1723    dlMsgSchInfo->transportBlock[0].ndi = hqP->tbInfo[0].ndi; /*How to handle two tb case?TBD*/
1724    dlMsgSchInfo->harqProcNum = hqP->procId;
1725    dlMsgSchInfo->dlAssignIdx = 0;
1726    dlMsgSchInfo->pucchTpc = 0;
1727    dlMsgSchInfo->pucchResInd = 0;
1728    dlMsgSchInfo->harqFeedbackInd = hqP->k1;
1729    dlMsgSchInfo->dciFormatId = 1;
1730 }
1731
1732 /*******************************************************************
1733  *
1734  * @brief sch Process pending Msg4 Req
1735  *
1736  * @details
1737  *
1738  *    Function : schProcessMsg4Req
1739  *
1740  *    Functionality:
1741  *       sch Process pending Msg4 Req
1742  *
1743  * @params[in] SchCellCb *cell, cell cb struct pointer
1744  * @params[in] SlotTimingInfo currTime, current timing info
1745  * @params[in] uint8_t ueId, ue ID
1746  * @params[in] bool isRetxMsg4, indicator to MSG4 retransmission
1747  * @params[in] SchDlHqProcCb **msg4HqProc, address of MSG4 HARQ proc pointer
1748  * @return ROK     - success
1749  *         RFAILED - failure
1750  *
1751  *******************************************************************/
1752
1753 uint8_t schProcessMsg4Req(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId, bool isRetxMsg4, SchDlHqProcCb **msg4HqProc)
1754 {
1755    uint8_t pdschStartSymbol = 0, pdschNumSymbols = 0;
1756    SlotTimingInfo pdcchTime, pdschTime, pucchTime;
1757    DlMsgSchInfo *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for Msg4 */
1758    DlMsgSchInfo *msg4SlotAlloc = NULLP;   /* Stores info for transmission of PDSCH for Msg4 */
1759
1760    if(cell == NULL)
1761    {
1762       DU_LOG("\nERROR  -->  SCH: schProcessMsg4Req() : Cell is NULL");
1763       return RFAILED;
1764    }
1765
1766    if (isRetxMsg4 == FALSE)
1767    {
1768       if (RFAILED == schDlGetAvlHqProcess(cell, &cell->ueCb[ueId - 1], msg4HqProc))
1769       {
1770          DU_LOG("\nERROR  -->  SCH: schProcessMsg4Req() : No process");
1771          return RFAILED;
1772       }
1773    }
1774
1775    if(findValidK0K1Value(cell, currTime, ueId, false, &pdschStartSymbol, &pdschNumSymbols, &pdcchTime, &pdschTime,\
1776             &pucchTime, isRetxMsg4, *msg4HqProc) != true )
1777    {
1778       DU_LOG("\nERROR  -->  SCH: schProcessMsg4Req() : k0 k1 not found");
1779       return RFAILED;
1780    }
1781
1782    if(cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] == NULL)
1783    {
1784       SCH_ALLOC(dciSlotAlloc, sizeof(DlMsgSchInfo));
1785       if(dciSlotAlloc == NULLP)
1786       {
1787          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
1788          return RFAILED;
1789       }
1790       cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = dciSlotAlloc;
1791       memset(dciSlotAlloc, 0, sizeof(DlMsgSchInfo));
1792    }
1793    else
1794       dciSlotAlloc = cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1];
1795
1796    /* Fill PDCCH and PDSCH scheduling information for Msg4 */
1797    if((schDlRsrcAllocMsg4(cell, pdschTime, ueId, dciSlotAlloc, pdschStartSymbol, pdschNumSymbols, isRetxMsg4, *msg4HqProc)) != ROK)
1798    {
1799       DU_LOG("\nERROR  -->  SCH: Scheduling of Msg4 failed in slot [%d]", pdschTime.slot);
1800       if(!dciSlotAlloc->dlMsgPdschCfg)
1801       {
1802          SCH_FREE(dciSlotAlloc, sizeof(DlMsgSchInfo));
1803          cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1804       }
1805       return RFAILED;
1806    }
1807
1808    /* Check if both DCI and RAR are sent in the same slot.
1809     * If not, allocate memory RAR PDSCH slot to store RAR info
1810     */
1811    if(pdcchTime.slot == pdschTime.slot)
1812    {
1813       SCH_ALLOC(dciSlotAlloc->dlMsgPdschCfg, sizeof(PdschCfg));
1814       if(!dciSlotAlloc->dlMsgPdschCfg)
1815       {
1816          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc->dlMsgPdschCfg");
1817          SCH_FREE(dciSlotAlloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
1818          SCH_FREE(dciSlotAlloc, sizeof(DlMsgSchInfo));
1819          cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1820          return RFAILED;
1821       }
1822       memcpy(dciSlotAlloc->dlMsgPdschCfg, &dciSlotAlloc->dlMsgPdcchCfg->dci.pdschCfg, sizeof(PdschCfg));
1823    }
1824    else
1825    {
1826       /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
1827       if(cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1] == NULL)
1828       {
1829          SCH_ALLOC(msg4SlotAlloc, sizeof(DlMsgSchInfo));
1830          if(msg4SlotAlloc == NULLP)
1831          {
1832             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4SlotAlloc");
1833             SCH_FREE(dciSlotAlloc->dlMsgPdcchCfg, sizeof(PdcchCfg));
1834             if(!dciSlotAlloc->dlMsgPdschCfg)
1835             {
1836                SCH_FREE(dciSlotAlloc, sizeof(DlMsgSchInfo));
1837                cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1838             }
1839             return RFAILED;
1840          }
1841          cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1] = msg4SlotAlloc;
1842          memset(msg4SlotAlloc, 0, sizeof(DlMsgSchInfo));
1843       }
1844       else
1845          msg4SlotAlloc = cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1];
1846
1847       /* Copy all msg4 pdschcfg info */
1848       msg4SlotAlloc->crnti =dciSlotAlloc->crnti;
1849       msg4SlotAlloc->bwp = dciSlotAlloc->bwp;
1850       SCH_ALLOC(msg4SlotAlloc->dlMsgPdschCfg, sizeof(PdschCfg));
1851       if(msg4SlotAlloc->dlMsgPdschCfg)
1852       {
1853          memcpy(msg4SlotAlloc->dlMsgPdschCfg, &dciSlotAlloc->dlMsgPdcchCfg->dci.pdschCfg, sizeof(PdschCfg));
1854       }
1855       else
1856       {
1857          SCH_FREE(dciSlotAlloc->dlMsgPdcchCfg, sizeof(PdcchCfg));    
1858          if(dciSlotAlloc->dlMsgPdschCfg == NULLP)
1859          {
1860             SCH_FREE(dciSlotAlloc, sizeof(DlMsgSchInfo));
1861             cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1862
1863          }
1864          SCH_FREE(msg4SlotAlloc, sizeof(DlMsgSchInfo));
1865          cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1866          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4SlotAlloc->dlMsgPdschCfg");
1867          return RFAILED;
1868       }
1869    }
1870
1871    /* PUCCH resource */
1872    schAllocPucchResource(cell, pucchTime, cell->raCb[ueId-1].tcrnti, &cell->ueCb[ueId-1], isRetxMsg4, *msg4HqProc);
1873
1874    cell->schDlSlotInfo[pdcchTime.slot]->pdcchUe = ueId;
1875    cell->schDlSlotInfo[pdschTime.slot]->pdschUe = ueId;
1876    cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
1877    cell->raCb[ueId-1].msg4recvd = FALSE;
1878    if(isRetxMsg4)
1879    {
1880       cell->ueCb[ueId-1].retxMsg4HqProc= NULLP;
1881    }
1882    return ROK;
1883 }
1884
1885 /*******************************************************************
1886  *
1887  * @brief sch Process pending Sr or Bsr Req
1888  *
1889  * @details
1890  *
1891  *    Function : updateBsrAndLcList
1892  *
1893  *    Functionality:
1894  *       Updating the BSRInfo in UECB and Lclist
1895  *
1896  * @params[in] SchCellCb *cell,  SlotTimingInfo currTime 
1897  * @return ROK     - success
1898  *         RFAILED - failure
1899  *
1900  *******************************************************************/
1901 void updateBsrAndLcList(CmLListCp *lcLL, BsrInfo *bsrInfo, uint8_t status)
1902 {
1903    CmLList *node = NULLP, *next = NULLP;
1904    LcInfo *lcNode = NULLP;
1905
1906    if(lcLL == NULLP)
1907    {
1908       DU_LOG("\nERROR --> SCH: LcList not present");
1909       return;
1910    }
1911
1912    if(lcLL->count)
1913    {
1914       node = lcLL->first;
1915    }
1916    else
1917    {
1918       /*lcLL is empty*/
1919       return;
1920    }
1921
1922    while(node)
1923    {
1924       next = node->next;
1925       lcNode = (LcInfo *)node->node;
1926       if(lcNode != NULLP)
1927       {
1928           /*Only when Status is OK then allocation is marked as ZERO and reqBO
1929            * is updated in UE's DB. If Failure, then allocation is added to reqBO 
1930            * and same is updated in Ue's DB inside BSR Info structure*/
1931          if(status == ROK)
1932          {
1933             lcNode->allocBO = 0;
1934          }
1935
1936          lcNode->reqBO += lcNode->allocBO;
1937          bsrInfo[lcNode->lcId].dataVol = lcNode->reqBO;
1938          if(lcNode->reqBO == 0)
1939          {
1940             handleLcLList(lcLL, lcNode->lcId, DELETE);
1941          }
1942       }
1943       node = next;
1944    }
1945 }
1946
1947 /*******************************************************************
1948  *
1949  * @brief sch Process pending Sr or Bsr Req
1950  *
1951  * @details
1952  *
1953  *    Function : schProcessSrOrBsrReq
1954  *
1955  *    Functionality:
1956  *       sch Process pending Sr or Bsr Req
1957  *
1958  * @params[in] SchCellCb *cell,  SlotTimingInfo currTime
1959  * @params[in] uint8_t ueId, Bool isRetx, SchUlHqProcCb **hqP
1960  * @return true  - success
1961  *         false - failure
1962  *
1963  *******************************************************************/
1964 bool schProcessSrOrBsrReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId, bool isRetx, SchUlHqProcCb **hqP)
1965 {
1966    bool k2Found = FALSE;
1967    uint8_t startSymb = 0, symbLen = 0;
1968    uint8_t k2TblIdx = 0, k2Index = 0, k2Val = 0;
1969    SchUeCb *ueCb;
1970    SchK2TimingInfoTbl *k2InfoTbl=NULLP;
1971    SlotTimingInfo dciTime, puschTime;
1972    
1973    if(cell == NULLP)
1974    {
1975       DU_LOG("\nERROR  -->  SCH: schProcessSrOrBsrReq() : Cell is NULL");
1976       return false;
1977    }
1978
1979    ueCb = &cell->ueCb[ueId-1];
1980
1981    if(ueCb == NULLP)
1982    {
1983       DU_LOG("\nERROR  -->  SCH: schProcessSrOrBsrReq() : UE is NULL");
1984       return false;
1985    }
1986
1987    if (isRetx == FALSE)
1988    {
1989       if (schUlGetAvlHqProcess(cell, ueCb, hqP) != ROK)
1990       {
1991          return RFAILED;
1992       }
1993    }
1994
1995    /* Calculating time frame to send DCI for SR */
1996    ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
1997 #ifdef NR_TDD
1998    if(schGetSlotSymbFrmt(dciTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
1999 #endif
2000    {     
2001       if(ueCb->k2TblPrsnt)
2002          k2InfoTbl = &ueCb->k2InfoTbl;
2003       else
2004          k2InfoTbl =  &cell->k2InfoTbl;
2005
2006       for(k2TblIdx = 0; k2TblIdx < k2InfoTbl->k2TimingInfo[dciTime.slot].numK2; k2TblIdx++)
2007       {
2008          k2Index = k2InfoTbl->k2TimingInfo[dciTime.slot].k2Indexes[k2TblIdx];
2009
2010          if(!ueCb->k2TblPrsnt)
2011          {
2012             k2Val = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
2013             startSymb = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
2014             symbLen = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
2015          }
2016          else
2017          {
2018             k2Val = ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].k2;
2019             startSymb =  ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].startSymbol;
2020             symbLen =  ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].symbolLength;
2021          }
2022          /* Check for number of Symbol of PUSCH should be same as original in case of transmisson*/
2023          /* Calculating time frame to send PUSCH for SR */
2024          ADD_DELTA_TO_TIME(dciTime, puschTime, k2Val, cell->numSlots);
2025 #ifdef NR_TDD
2026          if(schGetSlotSymbFrmt(puschTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
2027             continue;
2028 #endif
2029          if(cell->schUlSlotInfo[puschTime.slot]->puschUe != 0)
2030          {
2031             continue;
2032          }
2033          k2Found = true;
2034          if(hqP)
2035          {
2036             ADD_DELTA_TO_TIME(puschTime, (*hqP)->puschTime, 0, cell->numSlots);
2037          }
2038          break;
2039       }
2040    }
2041    
2042    if(k2Found == true)
2043    {
2044       if(cell->api->SchScheduleUlLc(dciTime, puschTime, startSymb, symbLen, isRetx, hqP) != ROK)
2045          return false;
2046    }
2047    else
2048    {
2049       DU_LOG("\nDEBUG  -->  SCH : schProcessSrOrBsrReq(): K2 value is not found");
2050       return false;     
2051    }
2052    return true;
2053 }
2054
2055 /********************************************************************************
2056  *
2057  * @brief Increment the Slot by a input factor
2058  *
2059  * @details
2060  *
2061  *    Function : schIncrSlot
2062  *
2063  *    Functionality:
2064  *       Increment the slot by a input factor till num of Slots configured in a
2065  *       Radio Frame. If it exceeds, move to next sfn.
2066  *
2067  * @params[in/out] SlotTimingInfo timingInfo
2068  *        [in]     uint8_t incr [Increment factor]
2069  *        [in]     numSlotsPerRF [Number of Slots configured per RF as per
2070  *                                numerology]
2071  * @return ROK     - success
2072  *         RFAILED - failure
2073  *
2074  *******************************************************************/
2075 void schIncrSlot(SlotTimingInfo *timingInfo, uint8_t incr, uint16_t numSlotsPerRF)
2076 {
2077    timingInfo->slot += incr;
2078    if(timingInfo->slot >= numSlotsPerRF)
2079    {
2080       timingInfo->sfn += timingInfo->slot/numSlotsPerRF;
2081       timingInfo->slot %= numSlotsPerRF;
2082       if(timingInfo->sfn >  MAX_SFN)
2083       {
2084          timingInfo->sfn %= MAX_SFN;
2085       }
2086    }
2087 }
2088
2089 /*******************************************************************
2090 *
2091 * @brief   Fill PDSCH info in Page Alloc
2092 *
2093 * @details
2094 *
2095 *    Function : schFillPagePdschCfg 
2096 *
2097 *    Functionality: Fill PDSCH info in Page Alloc
2098 *
2099 * @params[in] SchCellCb *cell, PdschCfg *pagePdschCfg, SlotTimingInfo slotTime, 
2100 *             uint16_t tbsSize, uint8_t mcs, uint16_t startPrb
2101 *
2102 * @return pointer to return Value(ROK, RFAILED) 
2103 *
2104 * ****************************************************************/
2105 uint8_t schFillPagePdschCfg(SchCellCb *cell, PageDlSch *pageDlSch, SlotTimingInfo slotTime, uint16_t tbSize, uint8_t mcs, uint16_t startPrb)
2106 {
2107    uint8_t dmrsStartSymbol, startSymbol, numSymbol;
2108
2109    /* fill the PDSCH PDU */
2110
2111    pageDlSch->tbInfo.mcs = mcs;
2112    tbSize = tbSize + TX_PAYLOAD_HDR_LEN;
2113    pageDlSch->tbInfo.tbSize = tbSize;
2114    pageDlSch->dmrs.dmrsType = 0; /* type-1 */
2115    pageDlSch->dmrs.nrOfDmrsSymbols               = NUM_DMRS_SYMBOLS;
2116    pageDlSch->dmrs.dmrsAddPos                    = DMRS_ADDITIONAL_POS;
2117
2118    /* the RB numbering starts from coreset0, and PDSCH is always above SSB */
2119    pageDlSch->freqAlloc.startPrb  = startPrb;
2120    pageDlSch->freqAlloc.numPrb    = schCalcNumPrb(tbSize, mcs, NUM_PDSCH_SYMBOL);
2121    pageDlSch->vrbPrbMapping       = 0; /* non-interleaved */
2122    /* This is Intel's requirement. PDSCH should start after PDSCH DRMS symbol */
2123    pageDlSch->timeAlloc.mappingType         = DMRS_MAP_TYPE_A; /* Type-A */
2124    pageDlSch->timeAlloc.startSymb = 3; /* spec-38.214, Table 5.1.2.1-1 */
2125    pageDlSch->timeAlloc.numSymb   = NUM_PDSCH_SYMBOL;
2126
2127    /* Find total symbols occupied including DMRS */
2128    dmrsStartSymbol = findDmrsStartSymbol(4);
2129    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
2130     * in that case only PDSCH symbols are marked as occupied */
2131    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
2132    {
2133       startSymbol = pageDlSch->timeAlloc.startSymb;
2134       numSymbol = pageDlSch->timeAlloc.numSymb;
2135    }
2136    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
2137    else
2138    {
2139       startSymbol = dmrsStartSymbol;
2140       numSymbol = pageDlSch->dmrs.nrOfDmrsSymbols + pageDlSch->timeAlloc.numSymb;
2141    }
2142
2143    /* Allocate the number of PRBs required for DL PDSCH */
2144    if((allocatePrbDl(cell, slotTime, startSymbol, numSymbol,\
2145                &pageDlSch->freqAlloc.startPrb, pageDlSch->freqAlloc.numPrb)) != ROK)
2146    {
2147       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for DL MSG");
2148       return RFAILED;
2149    }
2150    return ROK;
2151 }
2152
2153 /**
2154  * @brief Handles retransmission for MSG3
2155  *
2156  * @details
2157  *
2158  *     Function : schMsg3RetxSchedulingForUe
2159  *      
2160  *      This function handles retransmission for MSG3
2161  *           
2162  *  @param[in]  SchRaCb *raCb, RA cb pointer
2163  *  @return  
2164  *      -# ROK
2165  *      -# RFAILED
2166  **/
2167 uint8_t schMsg3RetxSchedulingForUe(SchRaCb *raCb)
2168 {
2169    bool      k2Found = false;
2170    uint16_t             dciSlot = 0;
2171    SlotTimingInfo       dciTime, msg3Time;
2172    SchCellCb            *cell = NULLP;
2173    SlotTimingInfo       currTime;
2174    DciInfo  *dciInfo = NULLP;
2175    cell = raCb->cell;
2176    currTime = cell->slotInfo;
2177
2178    /* Calculating time frame to send DCI for MSG3 Retx*/
2179    ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
2180 #ifdef NR_TDD
2181    /* Consider this slot for sending DCI, only if it is a DL slot */
2182    if(schGetSlotSymbFrmt(dciSlot, raCb->cell->slotFrmtBitMap) == DL_SLOT)
2183 #endif
2184    {
2185       /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
2186       if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
2187          return false;
2188
2189       k2Found = schGetMsg3K2(cell, &raCb->msg3HqProc, dciTime.slot, &msg3Time, TRUE);
2190
2191       if (!k2Found)
2192       {
2193          return RFAILED;
2194       }
2195       SCH_ALLOC(dciInfo, sizeof(DciInfo));
2196       if(!dciInfo)
2197       {
2198          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
2199          return RFAILED;
2200       }
2201       cell->schDlSlotInfo[msg3Time.slot]->ulGrant = dciInfo;
2202       SCH_ALLOC(cell->schUlSlotInfo[msg3Time.slot]->schPuschInfo, sizeof(SchPuschInfo));
2203       memset(dciInfo,0,sizeof(DciInfo));
2204       schFillUlDciForMsg3Retx(raCb, cell->schUlSlotInfo[msg3Time.slot]->schPuschInfo, dciInfo);
2205    }   
2206    raCb->retxMsg3HqProc = NULLP;
2207    return ROK;
2208 }
2209
2210 /**
2211  * @brief Get K2 value for MSG3
2212  *
2213  * @details
2214  *
2215  *     Function : schGetMsg3K2
2216  *      
2217  *      This function gets K2 for MSG3
2218  *           
2219  *  @param[in]  SchCellCb *cell, Cell cb struc pointer
2220  *  @param[in]  SchUlHqProcCb* msg3HqProc, msg3 harq proc pointer
2221  *  @param[in]  uint16_t dlTime, DL time of scheduling
2222  *  @param[in]  SlotTimingInfo *msg3Time, MSG3 timing info
2223  *  @param[in]  bool isRetx, indicates MSG3 retransmission
2224  *  @return  
2225  *      -# true
2226  *      -# false
2227  **/
2228 bool schGetMsg3K2(SchCellCb *cell, SchUlHqProcCb* msg3HqProc, uint16_t dlTime, SlotTimingInfo *msg3Time, bool isRetx)
2229 {
2230    bool      k2Found = false;
2231    uint8_t   k2TblIdx = 0;
2232    uint8_t   k2Index = 0;
2233    uint8_t   k2 = 0;
2234    uint8_t   numK2 = 0;
2235    uint8_t   puschMu = 0;
2236    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
2237 #ifdef NR_TDD
2238    uint8_t   totalCfgSlot = 0;
2239 #endif
2240    SchK2TimingInfoTbl   *msg3K2InfoTbl=NULLP;
2241    SlotTimingInfo       currTime, msg3TempTime;
2242    currTime = cell->slotInfo;
2243    puschMu = cell->numerology;
2244
2245    if (isRetx)
2246    {
2247       if(!msg3HqProc)
2248          return false;
2249
2250       numK2 = cell->k2InfoTbl.k2TimingInfo[dlTime].numK2;
2251       msg3K2InfoTbl = &cell->msg3K2InfoTbl;
2252       msg3MinSchTime = 0;
2253       msg3Delta = 0;
2254    }
2255    else
2256    {
2257       numK2 = cell->msg3K2InfoTbl.k2TimingInfo[dlTime].numK2;
2258       msg3K2InfoTbl = &cell->k2InfoTbl;
2259       msg3MinSchTime = minMsg3SchTime[cell->numerology];
2260       msg3Delta = puschDeltaTable[puschMu];
2261    }
2262
2263    for(k2TblIdx = 0; k2TblIdx < numK2; k2TblIdx++)
2264    {
2265       k2Index = msg3K2InfoTbl->k2TimingInfo[dlTime].k2Indexes[k2TblIdx];
2266
2267       k2 = cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
2268       if (isRetx)
2269       {
2270          if ((msg3HqProc->strtSymbl != cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol) ||
2271             (msg3HqProc->numSymbl != cell->cellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength))
2272          {
2273             continue;
2274          }
2275       }
2276       /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
2277       k2 = k2 + msg3Delta;
2278       if(k2 >= msg3MinSchTime)
2279       {
2280          ADD_DELTA_TO_TIME(currTime, msg3TempTime, k2, cell->numSlots);
2281 #ifdef NR_TDD
2282          if(schGetSlotSymbFrmt(msg3TempTime.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
2283             continue;
2284 #endif
2285          /* If PUSCH is already scheduled on this slot, another PUSCH
2286           * pdu cannot be scheduled here */
2287          if(cell->schUlSlotInfo[msg3TempTime.slot]->puschUe != 0)
2288             continue;
2289          k2Found = true;
2290          break;
2291       }
2292    }
2293    if (k2Found == true)
2294    {
2295       msg3Time->slot = msg3TempTime.slot;
2296       msg3Time->sfn = msg3TempTime.sfn;
2297       msg3Time->slot = msg3TempTime.slot;
2298    }
2299    return k2Found;
2300 }
2301
2302 /**********************************************************************
2303   End of file
2304  **********************************************************************/
2305