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