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