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