[Epic-ID: ODUHIGH-488][Task-ID: ODUHIGH-491] WG8 Alignment [UL Scheduling Information]
[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    ResAllocType1 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.resAlloc.type1.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.resAlloc.type1.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                ulSchedPucch->addDmrs = formatCfg->addDmrs;
297                ulSchedPucch->pi2BPSK = formatCfg->pi2BPSK;
298             }
299             return ret;
300          }/* To Add support for more Pucch Format */
301
302       default:
303          DU_LOG("\nERROR  --> SCH : Invalid PUCCH format[%d] in fillUlSchedPucchFormatCfg()", pucchFormat);
304          ret = RFAILED;
305          return ret;
306    }
307    return ret;
308 }
309
310 /**
311  * @brief Function to fill Pucch Dedicated Cfg for UL Sched Info
312  *
313  * @details
314  *
315  *     Function : fillUlSchedPucchDedicatedCfg
316  *     
317  *     Function to fill Pucch Dedicated Cfg for UL Sched Info
318  *     
319  *  @param[in]  pucchFormat to be filled
320  *  @param[in]  SchPucchFormatCfg pointer, SchPucchCfg pointer
321  *  @return  void
322  **/
323
324 uint8_t fillUlSchedPucchDedicatedCfg(SchCellCb *cell, SchPucchCfg *pucchDedCfg,\
325    SlotTimingInfo *slotInfo, SchPucchInfo *ulSchedPucch)
326 {
327    uint8_t ret, resrcSetIdx, resrcIdx, schedReqIdx, srPeriodicity = 0;
328    uint16_t srOffset = 0;
329    uint16_t numSlots = cell->numSlots;
330    bool isAllocated = false;
331    uint16_t pucchStartPrb;
332    ret = ROK;
333    if(pucchDedCfg->resrcSet && pucchDedCfg->resrc)
334    {
335       //Assuming one entry in the list
336       for(resrcSetIdx = 0; resrcSetIdx < pucchDedCfg->resrcSet->resrcSetToAddModListCount; resrcSetIdx++)
337       {
338          for(resrcIdx = 0; resrcIdx < pucchDedCfg->resrc->resrcToAddModListCount; resrcIdx++)
339          {
340             if(pucchDedCfg->resrcSet->resrcSetToAddModList[resrcSetIdx].resrcList[resrcSetIdx] ==\
341                   pucchDedCfg->resrc->resrcToAddModList[resrcIdx].resrcId)
342             {
343                ulSchedPucch->intraFreqHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].intraFreqHop;
344                ulSchedPucch->secondPrbHop = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].secondPrbHop;
345                ulSchedPucch->fdAlloc.resAlloc.type1.startPrb = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].startPrb;
346                ulSchedPucch->pucchFormat = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].pucchFormat;
347                ret = fillUlSchedPucchFormat(ulSchedPucch->pucchFormat, ulSchedPucch,\
348                      &pucchDedCfg->resrc->resrcToAddModList[resrcIdx], NULLP);
349                if(ret == RFAILED)
350                   return ret;
351
352                pucchStartPrb = pucchDedCfg->resrc->resrcToAddModList[resrcIdx].startPrb;
353                ret = allocatePrbUl(cell, *slotInfo, ulSchedPucch->tdAlloc.startSymb, ulSchedPucch->tdAlloc.numSymb, &pucchStartPrb, PUCCH_NUM_PRB_FORMAT_0_1_4);
354                if(ret == ROK)
355                {
356                   isAllocated = true;
357                   break;
358                }
359             }
360          }
361          if(isAllocated)
362          break;
363       }
364    }
365
366    if(pucchDedCfg->format1)
367    {
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       }
391    }
392    return ret;
393 }
394
395 /**
396  * @brief Function to fill Pucch Resource Info
397  *
398  * @details
399  *
400  *     Function : fillPucchResourceInfo
401  *     
402  *     Function to fill Pucch Resource Info
403  *     
404  *  @param[in]  SchPucchInfo *schPucchInfo, Inst inst
405  *  @return  ROK/RFAILED
406  **/
407
408 uint16_t fillPucchResourceInfo(uint8_t ueId, SchPucchInfo *schPucchInfo, Inst inst, SlotTimingInfo slotInfo)
409 {
410    uint8_t ret = ROK,  ueIdx = 0, pucchIdx = 0;
411    SchCellCb  *cell = schCb[inst].cells[inst];
412    SchPucchCfgCmn *pucchCfg = NULLP;
413    SchBwpParams *ulBwp = NULLP;
414 #ifdef NR_DRX 
415    SchUeCb *ueCb = NULLP;
416 #endif
417    uint16_t startPrb;
418
419    ueIdx = ueId -1;
420 #ifdef NR_DRX 
421    ueCb = &(cell->ueCb[ueIdx]); 
422    if(ueCb->ueDrxInfoPres)
423    {
424       if(!ueCb->drxUeCb.drxUlUeActiveStatus)
425          return RFAILED;
426    }
427 #endif
428    if(cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfgPres)
429    {
430       /* fill pucch dedicated cfg */
431       ret = fillUlSchedPucchDedicatedCfg(cell,\
432        &cell->ueCb[ueIdx].ueCfg.spCellCfg.servCellRecfg.initUlBwp.pucchCfg, &slotInfo, schPucchInfo);
433       if(ret == RFAILED)
434       {
435          memset(schPucchInfo, 0, sizeof(SchPucchInfo));
436          DU_LOG("\nERROR  --> SCH : Filling PUCCH dedicated cfg failed at fillPucchResourceInfo()");
437          return ret;
438       }
439    }
440    else
441    {
442       /* fill pucch common cfg */
443       /* derive pucchResourceSet from schCellCfg */
444       pucchCfg = &cell->cellCfg.schInitialUlBwp.pucchCommon;
445       pucchIdx = pucchCfg->pucchResourceCommon;
446       ulBwp = &cell->cellCfg.schInitialUlBwp.bwp;
447       startPrb = ulBwp->freqAlloc.startPrb + pucchResourceSet[pucchIdx][3];
448       ret = allocatePrbUl(cell, slotInfo, pucchResourceSet[pucchIdx][1], pucchResourceSet[pucchIdx][2],\
449             &startPrb, PUCCH_NUM_PRB_FORMAT_0_1_4);
450       if (ret == ROK)
451       {
452          schPucchInfo->fdAlloc.resAlloc.type1.startPrb = ulBwp->freqAlloc.startPrb + pucchResourceSet[pucchIdx][3];
453          schPucchInfo->fdAlloc.resAlloc.type1.numPrb = PUCCH_NUM_PRB_FORMAT_0_1_4;
454          schPucchInfo->tdAlloc.startSymb = pucchResourceSet[pucchIdx][1];
455          schPucchInfo->tdAlloc.numSymb = pucchResourceSet[pucchIdx][2];
456          schPucchInfo->pucchFormat = pucchResourceSet[pucchIdx][0];
457
458          /* set SR and UCI flag to false */
459          schPucchInfo->srFlag  = true;
460       }
461    }
462    return ROK;
463 }
464
465 /**
466  * @brief resource allocation for UL
467  *
468  * @details
469  *
470  *     Function : schUlResAlloc
471  *     
472  *     This function handles UL Resource allocation
473  *     
474  *  @param[in]  SchCellCb *cell, cellCb
475  *  @return  void
476  **/
477 uint8_t schUlResAlloc(SchCellCb *cell, Inst schInst)
478 {
479    int ret = ROK;
480 #ifdef NR_DRX 
481    SchUeCb   *ueCb;
482 #endif
483    UlSchedInfo ulSchedInfo;
484    SchUlSlotInfo  *schUlSlotInfo = NULLP;
485    SlotTimingInfo ulTimingInfo;
486    memset(&ulSchedInfo, 0, sizeof(UlSchedInfo));
487
488    /* add PHY delta */
489    ADD_DELTA_TO_TIME(cell->slotInfo,ulTimingInfo,PHY_DELTA_UL+SCHED_DELTA, cell->numSlots);
490
491    ulSchedInfo.cellId = cell->cellId;
492    ulSchedInfo.slotIndInfo.cellId = ulSchedInfo.cellId;
493    ulSchedInfo.slotIndInfo.sfn = ulTimingInfo.sfn;
494    ulSchedInfo.slotIndInfo.slot = ulTimingInfo.slot;
495
496    /* Schedule resources for PRACH */
497    if(cell->firstSib1Transmitted)
498     schPrachResAlloc(cell, &ulSchedInfo, ulTimingInfo);
499
500    schUlSlotInfo = cell->schUlSlotInfo[ulTimingInfo.slot]; 
501    if(schUlSlotInfo->schPuschInfo)
502    {
503       GET_CRNTI(ulSchedInfo.crnti, schUlSlotInfo->puschUe);
504       /* Check the ue drx status if the UE is active for uplink scheduling or not  */
505 #ifdef NR_DRX 
506       ueCb = schGetUeCb(cell, ulSchedInfo.crnti);
507       if(ueCb->ueDrxInfoPres)
508       {
509          if(!ueCb->drxUeCb.drxUlUeActiveStatus)
510             return RFAILED;
511       }
512 #endif
513       ulSchedInfo.dataType |= SCH_DATATYPE_PUSCH;
514       memcpy(&ulSchedInfo.schPuschInfo, schUlSlotInfo->schPuschInfo,
515             sizeof(SchPuschInfo));
516       SCH_FREE(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
517       schUlSlotInfo->schPuschInfo = NULL;
518    }
519
520    if(schUlSlotInfo->pucchPres)
521    {
522       GET_CRNTI(ulSchedInfo.crnti, schUlSlotInfo->pucchUe);
523       ret = fillPucchResourceInfo(schUlSlotInfo->pucchUe, &schUlSlotInfo->schPucchInfo, schInst, ulTimingInfo);
524       if (ret == ROK)
525       {
526          ulSchedInfo.dataType |= SCH_DATATYPE_UCI;
527          memcpy(&ulSchedInfo.schPucchInfo, &schUlSlotInfo->schPucchInfo,
528                sizeof(SchPucchInfo));
529       }
530       else
531       {
532          return RFAILED;
533       }
534       memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
535    }
536
537    //send msg to MAC
538    ret = sendUlSchInfoToMac(&ulSchedInfo, schInst);
539    if(ret != ROK)
540    {
541       DU_LOG("\nERROR  -->  SCH : Sending UL Sch info from SCH to MAC failed");
542    }
543
544    schInitUlSlot(schUlSlotInfo);
545    return ret;
546 }
547
548 /*******************************************************************
549  *
550  * @brief Fills pdcch and pdsch info for msg4
551  *
552  * @details
553  *
554  *    Function : schDlRsrcAllocMsg4
555  *
556  *    Functionality:
557  *       Fills pdcch and pdsch info for msg4
558  *
559  * @params[in] SchCellCb *cell, SlotTimingInfo msg4Time
560  * @params[in] uint8_t ueId, DlMsgAlloc *dlMsgAlloc
561  * @params[in] uint8_t pdschStartSymbol, uint8_t pdschNumSymbols
562  * @params[in] bool isRetx, SchDlHqProcCb *hqP
563  * @return ROK     - success
564  *         RFAILED - failure
565  *
566  * ****************************************************************/
567 uint8_t schDlRsrcAllocMsg4(SchCellCb *cell, SlotTimingInfo msg4Time, uint8_t ueId, DlMsgAlloc *dlMsgAlloc,\
568                            uint8_t pdschStartSymbol, uint8_t pdschNumSymbols, bool isRetx, SchDlHqProcCb *hqP)
569 {
570    uint8_t coreset0Idx = 0;
571    uint8_t firstSymbol = 0;
572    uint8_t numSymbols = 0;
573    uint8_t mcs = DEFAULT_MCS;                         /* MCS fixed to 4 */
574    uint8_t dmrsStartSymbol = 0, startSymbol = 0, numSymbol = 0;
575    uint16_t tbSize = 0;
576    uint16_t numRbs;
577    SchBwpDlCfg *initialBwp = NULLP;
578    PdcchCfg *pdcch = NULLP;
579    PdschCfg *pdsch = NULLP;
580    BwpCfg *bwp = NULLP;
581    DlMsgSchInfo *msg4Alloc = NULLP;
582
583    if(cell == NULL)
584    {
585       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() : Cell is NULL");
586       return RFAILED;
587    }
588
589    if(dlMsgAlloc == NULL)
590    {
591       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() :  dlMsgAlloc is NULL");
592       return RFAILED;
593    }
594
595    msg4Alloc = &dlMsgAlloc->dlMsgSchedInfo[dlMsgAlloc->numSchedInfo];
596    initialBwp   = &cell->cellCfg.schInitialDlBwp;
597    pdcch = &msg4Alloc->dlMsgPdcchCfg;
598    pdsch = &msg4Alloc->dlMsgPdschCfg;
599    bwp = &msg4Alloc->bwp;
600    coreset0Idx  = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
601
602    fillDlMsgInfo(&msg4Alloc->dlMsgInfo, cell->raCb[ueId-1].tcrnti, isRetx, hqP);
603    msg4Alloc->dlMsgInfo.dlMsgPduLen = cell->raCb[ueId-1].dlMsgPduLen;
604
605    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
606    numRbs     = coresetIdxTable[coreset0Idx][1];
607    numSymbols = coresetIdxTable[coreset0Idx][2];
608
609    /* calculate time domain parameters */
610    uint16_t mask = 0x2000;
611    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
612    {
613       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
614          break;
615       else
616          mask = mask>>1;
617    }
618
619    /* fill BWP */
620    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
621    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
622    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
623    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
624
625    /* fill the PDCCH PDU */
626    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
627    pdcch->coresetCfg.durationSymbols = numSymbols;
628    memcpy(pdcch->coresetCfg.freqDomainResource, \
629       cell->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
630
631    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
632    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
633    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
634    pdcch->coresetCfg.coreSetType = 0;
635    pdcch->coresetCfg.coreSetSize = numRbs;
636    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
637    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
638    pdcch->numDlDci = 1;
639    pdcch->dci.rnti = cell->raCb[ueId-1].tcrnti;
640    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
641    pdcch->dci.scramblingRnti = 0;
642    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
643    pdcch->dci.aggregLevel = 4;
644    pdcch->dci.beamPdcchInfo.numPrgs = 1;
645    pdcch->dci.beamPdcchInfo.prgSize = 1;
646    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
647    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
648    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
649    pdcch->dci.txPdcchPower.powerValue = 0;
650    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
651    pdcch->dci.pdschCfg = pdsch;
652
653    /* fill the PDSCH PDU */
654    uint8_t cwCount = 0;
655    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
656    pdsch->rnti = cell->raCb[ueId-1].tcrnti;
657    pdsch->pduIndex = 0;
658    pdsch->numCodewords = 1;
659    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
660    {
661       pdsch->codeword[cwCount].targetCodeRate = 308;
662       pdsch->codeword[cwCount].qamModOrder = 2;
663       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
664       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
665       if(isRetx != TRUE)
666       {
667          tbSize = schCalcTbSize(msg4Alloc->dlMsgInfo.dlMsgPduLen + TX_PAYLOAD_HDR_LEN); /* MSG4 size + FAPI header size*/
668          hqP->tbInfo[cwCount].tbSzReq = tbSize;
669          pdsch->codeword[cwCount].rvIndex = 0;
670       }
671       else
672       {
673          pdsch->codeword[cwCount].rvIndex = (pdsch->codeword[cwCount].rvIndex +1) & 0x03;
674          tbSize = hqP->tbInfo[cwCount].tbSzReq;
675       }
676       pdsch->codeword[cwCount].tbSize = tbSize;
677    }
678    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
679    pdsch->numLayers = 1;
680    pdsch->transmissionScheme = 0;
681    pdsch->refPoint = 0;
682    pdsch->dmrs.dlDmrsSymbPos = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
683    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
684    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
685    pdsch->dmrs.scid = 0;
686    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
687    pdsch->dmrs.dmrsPorts = 0;
688    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
689    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
690    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
691
692    pdsch->pdschTimeAlloc.timeAlloc.startSymb = pdschStartSymbol; 
693    pdsch->pdschTimeAlloc.timeAlloc.numSymb = pdschNumSymbols;
694
695    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
696    pdsch->pdschFreqAlloc.freqAlloc.startPrb = MAX_NUM_RB;
697    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize, mcs, pdschNumSymbols);
698    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
699
700    /* Find total symbols occupied including DMRS */
701    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
702    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
703     * in that case only PDSCH symbols are marked as occupied */
704    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
705    {
706       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
707       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
708    }
709    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
710    else
711    {
712       startSymbol = dmrsStartSymbol;
713       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
714    }
715
716    /* Allocate the number of PRBs required for RAR PDSCH */
717    if((allocatePrbDl(cell, msg4Time, startSymbol, numSymbol,\
718       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
719    {
720       DU_LOG("\nERROR  --> SCH : Resource allocation failed for MSG4");
721       return RFAILED;
722    }
723
724    pdsch->beamPdschInfo.numPrgs = 1;
725    pdsch->beamPdschInfo.prgSize = 1;
726    pdsch->beamPdschInfo.digBfInterfaces = 0;
727    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
728    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
729    pdsch->txPdschPower.powerControlOffset = 0;
730    pdsch->txPdschPower.powerControlOffsetSS = 0;
731
732    msg4Alloc->dlMsgInfo.isMsg4Pdu = true;
733    return ROK;
734 }
735  
736 /*******************************************************************
737  *
738  * @brief Scheduling for Pucch Resource
739  *
740  * @details
741  *
742  *    Function : schAllocPucchResource
743  *
744  *    Functionality:
745  *       Scheduling for Pucch Resource
746  *
747  * @params[in] SchCellCb *cell, SlotTimingInfo pucchTime, crnti
748  * @params[in] SchUeCb *ueCb, bool isRetx, SchDlHqProcCb *hqP
749  * @return ROK     - success
750  *         RFAILED - failure
751  *
752  *******************************************************************/
753
754 uint16_t schAllocPucchResource(SchCellCb *cell, SlotTimingInfo pucchTime, uint16_t crnti,
755                                SchUeCb *ueCb, bool isRetx, SchDlHqProcCb *hqP)
756 {
757    uint16_t pucchSlot = 0;
758    SchUlSlotInfo  *schUlSlotInfo = NULLP;
759
760    pucchSlot = pucchTime.slot;
761    schUlSlotInfo = cell->schUlSlotInfo[pucchSlot];
762    memset(&schUlSlotInfo->schPucchInfo, 0, sizeof(SchPucchInfo));
763
764    schUlSlotInfo->pucchPres = true;
765    if(ueCb != NULLP)
766    {
767       /* set HARQ flag to true */
768       schUlSlotInfo->schPucchInfo.harqInfo.harqBitLength = 1; /* 1 bit for HARQ */
769       ADD_DELTA_TO_TIME(pucchTime, pucchTime, 3, cell->numSlots); /* SLOT_DELAY=3 */
770       cmLListAdd2Tail(&(ueCb->hqDlmap[pucchTime.slot]->hqList), &hqP->ulSlotLnk);
771    }
772    return ROK;
773 }
774
775 /*******************************************************************
776  *
777  * @brief Fills pdcch and pdsch info for dedicated DL msg
778  *
779  * @details
780  *
781  *    Function : schDlRsrcAllocDlMsg
782  *
783  *    Functionality:
784  *       Fills pdcch and pdsch info for dl msg
785  *
786  * @params[in] SchCellCb *cell, SlotTimingInfo slotTime
787  * @params[in] uint16_t crnti, uint32_t tbSize
788  * @params[in] DlMsgAlloc *dlMsgAlloc, uint16_t startPRB
789  * @params[in] uint8_t pdschStartSymbol, uint8_t pdschNumSymbols
790  * @params[in] bool isRetx, SchDlHqProcCb *hqP
791  * @return ROK     - success
792  *         RFAILED - failure
793  *
794  * ****************************************************************/
795 uint8_t schDlRsrcAllocDlMsg(SchCellCb *cell, SlotTimingInfo slotTime, uint16_t crnti,
796                 uint32_t tbSize, DlMsgAlloc *dlMsgAlloc, uint16_t startPRB, uint8_t pdschStartSymbol,
797                 uint8_t pdschNumSymbols, bool isRetx, SchDlHqProcCb *hqP)
798 {
799    uint8_t ueId=0;
800    PdcchCfg *pdcch = NULLP;
801    PdschCfg *pdsch = NULLP;
802    BwpCfg *bwp = NULLP;
803    SchUeCb ueCb;
804    SchControlRsrcSet coreset1;
805    SchPdschConfig pdschCfg;
806    uint8_t dmrsStartSymbol, startSymbol, numSymbol;
807
808    pdcch = &dlMsgAlloc->dlMsgSchedInfo[dlMsgAlloc->numSchedInfo].dlMsgPdcchCfg;
809    pdsch = &dlMsgAlloc->dlMsgSchedInfo[dlMsgAlloc->numSchedInfo].dlMsgPdschCfg;
810    bwp = &dlMsgAlloc->dlMsgSchedInfo[dlMsgAlloc->numSchedInfo].bwp;
811
812    GET_UE_ID(crnti, ueId);
813    ueCb  = cell->ueCb[ueId-1];
814    coreset1 = ueCb.ueCfg.spCellCfg.servCellRecfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
815    pdschCfg = ueCb.ueCfg.spCellCfg.servCellRecfg.initDlBwp.pdschCfg;
816
817    /* fill BWP */
818    bwp->freqAlloc.numPrb = MAX_NUM_RB;
819    bwp->freqAlloc.startPrb = 0;
820    bwp->subcarrierSpacing = cell->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
821    bwp->cyclicPrefix = cell->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
822
823    /* fill the PDCCH PDU */
824    //Considering coreset1 also starts from same symbol as coreset0
825    pdcch->coresetCfg.startSymbolIndex = coresetIdxTable[0][3];
826    pdcch->coresetCfg.durationSymbols = coreset1.duration;
827    memcpy(pdcch->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
828    pdcch->coresetCfg.cceRegMappingType = coreset1.cceRegMappingType; /* non-interleaved */
829    pdcch->coresetCfg.regBundleSize = 6;   /* must be 6 for non-interleaved */
830    pdcch->coresetCfg.interleaverSize = 0; /* NA for non-interleaved */
831    pdcch->coresetCfg.coreSetType = 1; /* non PBCH coreset */
832    //Considering number of RBs in coreset1 is same as coreset0
833    pdcch->coresetCfg.coreSetSize = coresetIdxTable[0][1];
834    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
835    pdcch->coresetCfg.precoderGranularity =  coreset1.precoderGranularity;
836    pdcch->numDlDci = 1;
837    pdcch->dci.rnti = ueCb.crnti;
838    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
839    pdcch->dci.scramblingRnti = 0;
840    pdcch->dci.cceIndex = 0; /* 0-3 for UL and 4-7 for DL */
841    pdcch->dci.aggregLevel = 4;
842    pdcch->dci.beamPdcchInfo.numPrgs = 1;
843    pdcch->dci.beamPdcchInfo.prgSize = 1;
844    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
845    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
846    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
847    pdcch->dci.txPdcchPower.powerValue = 0;
848    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
849
850    /* fill the PDSCH PDU */
851    uint8_t cwCount = 0;
852    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
853    pdsch->rnti = ueCb.crnti;
854    pdsch->pduIndex = 0;
855    pdsch->numCodewords = 1;
856    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
857    {
858       pdsch->codeword[cwCount].targetCodeRate = 308;
859       pdsch->codeword[cwCount].qamModOrder = ueCb.ueCfg.dlModInfo.modOrder;
860       pdsch->codeword[cwCount].mcsIndex = ueCb.ueCfg.dlModInfo.mcsIndex;
861       pdsch->codeword[cwCount].mcsTable = ueCb.ueCfg.dlModInfo.mcsTable;
862       pdsch->codeword[cwCount].rvIndex = 0;
863
864       if (isRetx != TRUE)
865       {
866          tbSize +=TX_PAYLOAD_HDR_LEN;
867          hqP->tbInfo[cwCount].tbSzReq = tbSize;
868       }
869       pdsch->codeword[cwCount].tbSize = tbSize;
870    }
871    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
872    pdsch->numLayers = 1;
873    pdsch->transmissionScheme = 0;
874    pdsch->refPoint = 0;
875    pdsch->dmrs.dlDmrsSymbPos = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
876    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
877    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
878    pdsch->dmrs.scid = 0;
879    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
880    pdsch->dmrs.dmrsPorts = 0;
881    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A; /* Setting to Type-A */
882    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
883    pdsch->dmrs.dmrsAddPos       = pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos;
884
885    pdsch->pdschTimeAlloc.timeAlloc.startSymb = pdschStartSymbol; 
886    pdsch->pdschTimeAlloc.timeAlloc.numSymb = pdschNumSymbols;
887
888    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
889    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
890    pdsch->pdschFreqAlloc.freqAlloc.startPrb = startPRB; /*Start PRB will be already known*/
891    pdsch->pdschFreqAlloc.freqAlloc.numPrb = schCalcNumPrb(tbSize, ueCb.ueCfg.dlModInfo.mcsIndex, pdschNumSymbols);
892
893    /* Find total symbols occupied including DMRS */
894    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
895    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
896     * in that case only PDSCH symbols are marked as occupied */
897    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
898    {
899       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
900       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
901    }
902    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
903    else
904    {
905       startSymbol = dmrsStartSymbol;
906       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
907    }
908
909    /* Allocate the number of PRBs required for DL PDSCH */
910    if((allocatePrbDl(cell, slotTime, startSymbol, numSymbol,\
911       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
912    {
913       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for DL MSG");
914       return RFAILED;
915    }
916
917    pdsch->beamPdschInfo.numPrgs = 1;
918    pdsch->beamPdschInfo.prgSize = 1;
919    pdsch->beamPdschInfo.digBfInterfaces = 0;
920    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
921    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
922    pdsch->txPdschPower.powerControlOffset = 0;
923    pdsch->txPdschPower.powerControlOffsetSS = 0;
924
925    pdcch->dci.pdschCfg = pdsch;
926    return ROK;
927 }
928
929 /*******************************************************************
930  *
931  * @brief Fills k0 and k1 information table for FDD 
932  *
933  * @details
934  *
935  *    Function : BuildK0K1TableForFdd 
936  *
937  *    Functionality:
938  *      Fills k0 and k1 information table for FDD
939  *
940  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
941  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
942  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
943  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
944  * @return ROK     - success
945  *         RFAILED - failure
946  *
947  * ****************************************************************/
948 void BuildK0K1TableForFdd(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres,SchPdschCfgCmn pdschCmnCfg,\
949 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
950 {
951    
952    uint8_t k1TmpVal =0, cfgIdx=0;
953    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, numTimeDomAlloc=0;
954    
955    /* TODO Commented these below lines for resolving warnings. Presently these variable are not 
956     * required but this will require for harq processing */
957    // uint8_t k0TmpVal = 0; 
958    // SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
959    // SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
960
961    /* Initialization the structure and storing the total slot values. */
962    memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
963    k0K1InfoTbl->tblSize = cell->numSlots;
964    
965    /* Storing time domain resource allocation list based on common or dedicated configuration. */
966    if(pdschCfgCmnPres == true)
967    {
968       numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
969       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
970       {
971          /*TODO uncomment this line during harq processing */
972          //cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
973       }
974    }
975    else
976    {
977       numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
978       for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
979       {
980          /*TODO uncomment this line during harq processing */
981          //dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
982       }
983    }
984    
985    /* Checking all the slots for K0 and K1 values. */
986    for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
987    {
988       numK0 = 0;
989       /* Storing the values of k0 based on time domain resource
990        * allocation list. If the value is unavailable then fill default values,
991        * As per 38.331 PDSCH-TimeDomainResourceAllocation field descriptions. */
992       for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX));  k0Index++)
993       {
994          /* TODO These if 0 we will remove during harq processing */
995 #if 0
996          if(pdschCfgCmnPres == true)
997          {
998             k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
999          }
1000          else
1001          {
1002             if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
1003             {
1004                k0TmpVal = *(dedTimeDomRsrcAllocList[k0Index].k0);
1005             }
1006             else
1007             { 
1008                k0TmpVal = DEFAULT_K0_VALUE;
1009             }
1010          }
1011 #endif         
1012          /* Checking all the Ul Alloc values. If value is less than MIN_NUM_K1_IDX
1013           * then skip else continue storing the values. */
1014          numK1 = 0;
1015          for(k1Index = 0; k1Index < ulAckListCount; k1Index++)
1016          {
1017             k1TmpVal = UlAckTbl[k1Index];
1018             if(k1TmpVal <= MIN_NUM_K1_IDX)
1019             {
1020                continue;
1021             }
1022
1023             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.k1Indexes[numK1++] = k1Index;
1024             /* TODO Store K1 index where harq feedback will be received in harq table. */ 
1025          }
1026          if(numK1)
1027          {
1028             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k1TimingInfo.numK1 = numK1;
1029             k0K1InfoTbl->k0k1TimingInfo[slotIdx].k0Indexes[numK0].k0Index = k0Index;
1030             numK0++;
1031          }
1032       }
1033       if(numK0)
1034       {
1035          k0K1InfoTbl->k0k1TimingInfo[slotIdx].numK0 = numK0;
1036       }
1037    }
1038 }
1039
1040 /*******************************************************************
1041  *
1042  * @brief Fills k0 and k1 information table  
1043  *
1044  * @details
1045  *
1046  *    Function : BuildK0K1Table
1047  *
1048  *    Functionality:
1049  *       Fills K0 and k1 information table 
1050  *
1051  * @params[in] SchCellCb *cell,SchK0K1TimingInfoTbl *k0K1InfoTbl,bool
1052  * pdschCfgCmnPres,uint8_t numTimeDomAlloc, SchPdschCfgCmnTimeDomRsrcAlloc
1053  * cmnTimeDomRsrcAllocList[], SchPdschTimeDomRsrcAlloc
1054  * dedTimeDomRsrcAllocList[], uint8_t ulAckListCount, uint8_t *UlAckTbl
1055  * @return ROK     - success
1056  *         RFAILED - failure
1057  *
1058  * ****************************************************************/
1059 void BuildK0K1Table(SchCellCb *cell, SchK0K1TimingInfoTbl *k0K1InfoTbl, bool pdschCfgCmnPres, SchPdschCfgCmn pdschCmnCfg,\
1060 SchPdschConfig pdschDedCfg, uint8_t ulAckListCount, uint8_t *UlAckTbl)
1061 {
1062
1063 #ifdef NR_TDD
1064    SlotConfig  slotCfg;
1065    bool ulSlotPresent = false;
1066    uint8_t k0TmpVal = 0, k1TmpVal =0, tmpSlot=0, startSymbol=0, endSymbol=0, checkSymbol=0;
1067    uint8_t slotIdx=0, k0Index=0, k1Index=0, numK0=0, numK1=0, cfgIdx=0, numTimeDomAlloc =0, totalCfgSlot =0;
1068    SchPdschCfgCmnTimeDomRsrcAlloc cmnTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
1069    SchPdschTimeDomRsrcAlloc dedTimeDomRsrcAllocList[MAX_NUM_DL_ALLOC];
1070 #endif
1071
1072    if(cell->cellCfg.dupMode == DUPLEX_MODE_FDD)
1073    {
1074       BuildK0K1TableForFdd(cell, k0K1InfoTbl, pdschCfgCmnPres, pdschCmnCfg, pdschDedCfg, ulAckListCount, UlAckTbl);
1075    }
1076    else
1077    {
1078 #ifdef NR_TDD
1079       
1080       /* Initialization the K0K1 structure, total num of slot and calculating the slot pattern length. */
1081       memset(k0K1InfoTbl, 0, sizeof(SchK0K1TimingInfoTbl));
1082       k0K1InfoTbl->tblSize = cell->numSlots;
1083       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
1084       
1085       /* Storing time domain resource allocation list based on common or 
1086        * dedicated configuration availability. */
1087       if(pdschCfgCmnPres == true)
1088       {
1089          numTimeDomAlloc = pdschCmnCfg.numTimeDomAlloc;
1090          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1091          {
1092             cmnTimeDomRsrcAllocList[cfgIdx] = pdschCmnCfg.timeDomRsrcAllocList[cfgIdx];
1093          }
1094       }
1095       else
1096       {
1097          numTimeDomAlloc = pdschDedCfg.numTimeDomRsrcAlloc;
1098          for(cfgIdx = 0; cfgIdx<numTimeDomAlloc; cfgIdx++)
1099          {
1100             dedTimeDomRsrcAllocList[cfgIdx] = pdschDedCfg.timeDomRsrcAllociList[cfgIdx];
1101          }
1102       }
1103
1104       /* Checking all possible indexes for K0 and K1 values. */
1105       for(slotIdx = 0; slotIdx < cell->numSlots; slotIdx++)
1106       {
1107          /* If current slot is UL or FLEXI then Skip because PDCCH is sent only in DL slots. */
1108          slotCfg = schGetSlotSymbFrmt(slotIdx%totalCfgSlot, cell->slotFrmtBitMap);
1109          if(slotCfg == UL_SLOT || slotCfg == FLEXI_SLOT)
1110          {
1111             continue;
1112          }
1113          
1114          /* Storing K0 , start symbol and length symbol for further processing.
1115           * If K0 value is not available then we can fill the default values
1116           * given in spec 38.331. */
1117          numK0 = 0;
1118          for(k0Index = 0; ((k0Index < numTimeDomAlloc) && (k0Index < MAX_NUM_K0_IDX)); k0Index++)
1119          {
1120             if(pdschCfgCmnPres == true)
1121             {
1122                k0TmpVal = cmnTimeDomRsrcAllocList[k0Index].k0;
1123                startSymbol = cmnTimeDomRsrcAllocList[k0Index].startSymbol;
1124                endSymbol = startSymbol + cmnTimeDomRsrcAllocList[k0Index].lengthSymbol;
1125             }
1126             else
1127             {
1128                if(dedTimeDomRsrcAllocList[k0Index].k0 != NULLP)
1129                {
1130                   k0TmpVal =  *(dedTimeDomRsrcAllocList[k0Index].k0);
1131                }
1132                else
1133                {
1134                   k0TmpVal = DEFAULT_K0_VALUE;
1135                }
1136                startSymbol = dedTimeDomRsrcAllocList[k0Index].startSymbol;
1137                endSymbol = startSymbol + dedTimeDomRsrcAllocList[k0Index].symbolLength;
1138             }
1139             
1140             /* If current slot + k0 is UL then skip the slot
1141              * else if it is DL slot then continue the next steps
1142              * else if it is a FLEXI slot then check symbols of slot, It should not
1143              * contain any UL slot. */
1144             tmpSlot = (slotIdx+k0TmpVal) % totalCfgSlot;
1145             slotCfg = schGetSlotSymbFrmt(tmpSlot, cell->slotFrmtBitMap);
1146             if(slotCfg == UL_SLOT)
1147             {
1148                continue;
1149             }
1150             if(slotCfg == FLEXI_SLOT)
1151             {
1152                for(checkSymbol = startSymbol; checkSymbol<endSymbol; checkSymbol ++)
1153                {
1154                   slotCfg = cell->cellCfg.tddCfg.slotCfg[tmpSlot][checkSymbol];
1155                   if(slotCfg == UL_SLOT)
1156                   {
1157                      continue;
1158                   }
1159                }
1160             }
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->cellCfg.tddCfg.slotCfg[tmpSlot][checkSymbol] == UL_SLOT)
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.ssbSchCfg.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.ssbSchCfg.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->cellCfg.tddCfg.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->cellCfg.tddCfg.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             /*The LC has been fully allocated, clean it*/
1651             if(lcNode->reqBO == 0)
1652             {
1653                handleLcLList(lcLL, lcNode->lcId, DELETE);
1654             }
1655          }
1656          else if(bsrInfo != NULLP)
1657          {
1658             *accumalatedBOSize += lcNode->allocBO;   
1659             DU_LOG("\nINFO   --> SCH: UL : LCID:%d,reqBO:%d, TotalBO Size:%d",\
1660                   lcNode->lcId,lcNode->reqBO, *accumalatedBOSize);
1661          }
1662       }
1663       node = next;
1664    }/*End of while*/
1665    return;
1666 }
1667
1668 /*******************************************************************
1669 *
1670 * @brief fill DL message information for MSG4 and Dedicated DL Msg
1671 *
1672 * @details
1673 *
1674 *    Function : fillDlMsgInfo
1675 *
1676 *    Functionality:
1677 *       fill DL message information for MSG4 and Dedicated DL Msg
1678 *
1679 * @params[in] DlMsgInfo *dlMsgInfo,  uint8_t crnti
1680 * @params[in] bool isRetx, SchDlHqProcCb *hqP
1681 * @return void
1682 *
1683 *******************************************************************/
1684 void fillDlMsgInfo(DlMsgInfo *dlMsgInfo, uint8_t crnti, bool isRetx, SchDlHqProcCb *hqP)
1685 {
1686    hqP->tbInfo[0].isEnabled = TRUE;
1687    hqP->tbInfo[0].state = HQ_TB_WAITING;
1688    hqP->tbInfo[0].txCntr++;
1689    hqP->tbInfo[1].isEnabled = TRUE;
1690    hqP->tbInfo[1].state = HQ_TB_WAITING;
1691    hqP->tbInfo[1].txCntr++;
1692    dlMsgInfo->crnti = crnti;
1693    dlMsgInfo->ndi = hqP->tbInfo[0].ndi; /*How to handle two tb case?TBD*/
1694    dlMsgInfo->harqProcNum = hqP->procId;
1695    dlMsgInfo->dlAssignIdx = 0;
1696    dlMsgInfo->pucchTpc = 0;
1697    dlMsgInfo->pucchResInd = 0;
1698    dlMsgInfo->harqFeedbackInd = hqP->k1;
1699    dlMsgInfo->dciFormatId = 1;
1700 }
1701
1702 /*******************************************************************
1703  *
1704  * @brief sch Process pending Msg4 Req
1705  *
1706  * @details
1707  *
1708  *    Function : schProcessMsg4Req
1709  *
1710  *    Functionality:
1711  *       sch Process pending Msg4 Req
1712  *
1713  * @params[in] SchCellCb *cell, cell cb struct pointer
1714  * @params[in] SlotTimingInfo currTime, current timing info
1715  * @params[in] uint8_t ueId, ue ID
1716  * @params[in] bool isRetxMsg4, indicator to MSG4 retransmission
1717  * @params[in] SchDlHqProcCb **msg4HqProc, address of MSG4 HARQ proc pointer
1718  * @return ROK     - success
1719  *         RFAILED - failure
1720  *
1721  *******************************************************************/
1722
1723 uint8_t schProcessMsg4Req(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId, bool isRetxMsg4, SchDlHqProcCb **msg4HqProc)
1724 {
1725    uint8_t pdschStartSymbol = 0, pdschNumSymbols = 0;
1726    SlotTimingInfo pdcchTime, pdschTime, pucchTime;
1727    DlMsgAlloc *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for Msg4 */
1728    DlMsgAlloc  *msg4SlotAlloc = NULLP;   /* Stores info for transmission of PDSCH for Msg4 */
1729
1730    if(cell == NULL)
1731    {
1732       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() : Cell is NULL");
1733       return RFAILED;
1734    }
1735
1736    if (isRetxMsg4 == FALSE)
1737    {
1738       if (RFAILED == schDlGetAvlHqProcess(cell, &cell->ueCb[ueId - 1], msg4HqProc))
1739       {
1740          DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() : No process");
1741          return RFAILED;
1742       }
1743    }
1744
1745    if(findValidK0K1Value(cell, currTime, ueId, false, &pdschStartSymbol, &pdschNumSymbols, &pdcchTime, &pdschTime,\
1746             &pucchTime, isRetxMsg4, *msg4HqProc) != true )
1747    {
1748       DU_LOG("\nERROR  -->  SCH: schDlRsrcAllocMsg4() : k0 k1 not found");
1749       return RFAILED;
1750    }
1751
1752    if(cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] == NULL)
1753    {
1754       SCH_ALLOC(dciSlotAlloc, sizeof(DlMsgAlloc));
1755       if(dciSlotAlloc == NULLP)
1756       {
1757          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
1758          return RFAILED;
1759       }
1760       cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = dciSlotAlloc;
1761       memset(dciSlotAlloc, 0, sizeof(DlMsgAlloc));
1762       GET_CRNTI(dciSlotAlloc->crnti, ueId);
1763    }
1764    else
1765       dciSlotAlloc = cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1];
1766
1767    /* Fill PDCCH and PDSCH scheduling information for Msg4 */
1768    if((schDlRsrcAllocMsg4(cell, pdschTime, ueId, dciSlotAlloc, pdschStartSymbol, pdschNumSymbols, isRetxMsg4, *msg4HqProc)) != ROK)
1769    {
1770       DU_LOG("\nERROR  -->  SCH: Scheduling of Msg4 failed in slot [%d]", pdschTime.slot);
1771       if(dciSlotAlloc->numSchedInfo == 0)
1772       {
1773          SCH_FREE(dciSlotAlloc, sizeof(DlMsgAlloc));
1774          cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1775       }
1776       else
1777          memset(&dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo], 0, sizeof(DlMsgSchInfo));
1778       return RFAILED;
1779    }
1780
1781    /* Check if both DCI and RAR are sent in the same slot.
1782     * If not, allocate memory RAR PDSCH slot to store RAR info
1783     */
1784    if(pdcchTime.slot == pdschTime.slot)
1785    {
1786       dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo].pduPres = BOTH;
1787       dciSlotAlloc->numSchedInfo++;
1788    }
1789    else
1790    {
1791       /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
1792       if(cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1] == NULL)
1793       {
1794          SCH_ALLOC(msg4SlotAlloc, sizeof(DlMsgAlloc));
1795          if(msg4SlotAlloc == NULLP)
1796          {
1797             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for msg4SlotAlloc");
1798             if(dciSlotAlloc->numSchedInfo == 0)
1799             {
1800                SCH_FREE(dciSlotAlloc, sizeof(DlMsgAlloc));
1801                cell->schDlSlotInfo[pdcchTime.slot]->dlMsgAlloc[ueId-1] = NULLP;
1802             }
1803             else
1804                memset(&dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo], 0, sizeof(DlMsgSchInfo));
1805             return RFAILED;
1806          }
1807          cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1] = msg4SlotAlloc;
1808          memset(msg4SlotAlloc, 0, sizeof(DlMsgAlloc));
1809          msg4SlotAlloc->crnti = dciSlotAlloc->crnti;
1810       }
1811       else
1812          msg4SlotAlloc = cell->schDlSlotInfo[pdschTime.slot]->dlMsgAlloc[ueId-1];
1813
1814       /* Copy all RAR info */
1815       memcpy(&msg4SlotAlloc->dlMsgSchedInfo[msg4SlotAlloc->numSchedInfo], \
1816          &dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo], sizeof(DlMsgSchInfo));
1817       msg4SlotAlloc->dlMsgSchedInfo[msg4SlotAlloc->numSchedInfo].dlMsgPdcchCfg.dci.pdschCfg = \
1818          &msg4SlotAlloc->dlMsgSchedInfo[msg4SlotAlloc->numSchedInfo].dlMsgPdschCfg;
1819
1820       /* Assign correct PDU types in corresponding slots */
1821       msg4SlotAlloc->dlMsgSchedInfo[msg4SlotAlloc->numSchedInfo].pduPres = PDSCH_PDU;
1822       dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo].pduPres = PDCCH_PDU;
1823       dciSlotAlloc->dlMsgSchedInfo[dciSlotAlloc->numSchedInfo].pdschSlot = pdschTime.slot;
1824
1825       dciSlotAlloc->numSchedInfo++;
1826       msg4SlotAlloc->numSchedInfo++;
1827    }
1828
1829    /* PUCCH resource */
1830    schAllocPucchResource(cell, pucchTime, cell->raCb[ueId-1].tcrnti, &cell->ueCb[ueId-1], isRetxMsg4, *msg4HqProc);
1831
1832    cell->schDlSlotInfo[pdcchTime.slot]->pdcchUe = ueId;
1833    cell->schDlSlotInfo[pdschTime.slot]->pdschUe = ueId;
1834    cell->schUlSlotInfo[pucchTime.slot]->pucchUe = ueId;
1835    cell->raCb[ueId-1].msg4recvd = FALSE;
1836    if(isRetxMsg4)
1837    {
1838       cell->ueCb[ueId-1].retxMsg4HqProc= NULLP;
1839    }
1840    return ROK;
1841 }
1842
1843 /*******************************************************************
1844  *
1845  * @brief  Handler to calculate TBS size for BSR requested
1846  *
1847  * @details
1848  *
1849  *    Function :  schCalculateUlTbs
1850  *
1851  *    Functionality: Function will note the required TBS for each LCGIDX and use
1852  *    the Priority LCG List and RRM policy to allocate the TBS size
1853  *
1854  * @params [in] ueCb (Pointer to UE CB)
1855  *         [in] puschTime (Time slot where PUSCH will be sent)
1856  *         [in] symbLen (No of Symbols used for PUSCH transmission)
1857  *         [out] startPrb(Pointer to startPRB which will be calculated while
1858  *         finding the best Free Block)
1859  *         [out] totTBS(Pointer to total TBS size)
1860  *         [in] isRetx (to indicate retransmission)
1861  *         [in] hqP (UL Harq process pointer)
1862  *
1863  * @return uint8_t : ROK > Scheduling of UL grant is successful
1864  *                   RFAILED > vice versa
1865  *
1866  * ****************************************************************/
1867 uint8_t schCalculateUlTbs(SchUeCb *ueCb, SlotTimingInfo puschTime, uint8_t symbLen,\
1868                           uint16_t *startPrb, uint32_t *totTBS, bool isRetx, SchUlHqProcCb *hqP)
1869 {
1870    uint16_t mcsIdx = 0;
1871    CmLListCp *lcLL = NULLP;
1872    uint16_t lcgIdx = 0, lcId =0, maxFreePRB = 0;
1873    uint16_t rsvdDedicatedPRB;
1874    *startPrb = 0;
1875    *totTBS = 0;
1876
1877    /* check for BSR */
1878    for(lcgIdx=0; lcgIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
1879    {
1880       if(ueCb->bsrInfo[lcgIdx].dataVol == 0)
1881       {
1882          continue;
1883       }
1884
1885       /*TODO: lcgIdx and LCID has been implemented as one to one mapping.
1886        * Need to check the mapping to figure out the LCID and lcgIdx once L2
1887        * spec specifies any logic*/
1888       lcId = lcgIdx;
1889       if(ueCb->ulInfo.ulLcCtxt[lcId].isDedicated)
1890       {
1891          lcLL = &(hqP->ulLcPrbEst.dedLcList);
1892          rsvdDedicatedPRB = ueCb->ulInfo.ulLcCtxt[lcId].rsvdDedicatedPRB;
1893       }
1894       else
1895       {
1896          lcLL = &(hqP->ulLcPrbEst.defLcList);
1897       }
1898
1899       /*[Step2]: Update the reqPRB and Payloadsize for this LC in the appropriate List*/
1900       if(updateLcListReqPRB(lcLL, lcId, ueCb->bsrInfo[lcgIdx].dataVol) != ROK)
1901       {
1902          DU_LOG("\nERROR  --> SCH: LcgId:%d updation failed",lcId);         
1903          return RFAILED;
1904       }
1905    }
1906
1907    if ((hqP->ulLcPrbEst.defLcList.count == 0) && (hqP->ulLcPrbEst.dedLcList.count == 0))
1908    {
1909       if( (ueCb->srRcvd) || (isRetx) )
1910       {
1911          *startPrb = MAX_NUM_RB;
1912          *totTBS = schCalcTbSize(UL_GRANT_SIZE);
1913       }
1914       /*Returning true when NO Grant is there for UE as this is not scheduling
1915        * error*/      
1916       return ROK;
1917    }
1918
1919    maxFreePRB = searchLargestFreeBlock(ueCb->cellCb, puschTime, startPrb, DIR_UL);
1920
1921    /*[Step4]: Estimation of PRB and BO which can be allocated to each LC in
1922     * the list based on RRM policy*/
1923
1924    /*Either this UE contains no reservedPRB pool fir dedicated S-NSSAI or 
1925     * Num of Free PRB available is not enough to reserve Dedicated PRBs*/
1926    if(maxFreePRB != 0)
1927    {
1928       mcsIdx = ueCb->ueCfg.ulModInfo.mcsIndex;
1929       if((hqP->ulLcPrbEst.dedLcList.count == 0) || ((maxFreePRB < rsvdDedicatedPRB)))
1930       {
1931          hqP->ulLcPrbEst.sharedNumPrb = maxFreePRB;
1932          DU_LOG("\nDEBUG  -->  SCH : UL Only Default Slice is scheduled, sharedPRB Count:%d",\
1933                hqP->ulLcPrbEst.sharedNumPrb);
1934
1935          /*PRB Alloc for Default LCs*/
1936          prbAllocUsingRRMPolicy(&(hqP->ulLcPrbEst.defLcList), FALSE, mcsIdx, symbLen,\
1937                &(hqP->ulLcPrbEst.sharedNumPrb), NULLP, NULLP,&(ueCb->srRcvd));
1938       }
1939       else
1940       {
1941          hqP->ulLcPrbEst.sharedNumPrb = maxFreePRB - rsvdDedicatedPRB;
1942
1943          /*PRB Alloc for Dedicated LCs*/
1944          prbAllocUsingRRMPolicy(&(hqP->ulLcPrbEst.dedLcList), TRUE, mcsIdx, symbLen,\
1945                &(hqP->ulLcPrbEst.sharedNumPrb), &(rsvdDedicatedPRB),\
1946                NULLP,&(ueCb->srRcvd));
1947
1948          /*PRB Alloc for Default LCs*/
1949          prbAllocUsingRRMPolicy(&(hqP->ulLcPrbEst.defLcList), FALSE, mcsIdx, symbLen, \
1950                &(hqP->ulLcPrbEst.sharedNumPrb), &(rsvdDedicatedPRB),\
1951                NULLP,&(ueCb->srRcvd));
1952       }
1953    }
1954    /*[Step5]:Traverse each LCID in LcList to calculate the exact Scheduled Bytes
1955     * using allocated BO per LC and Update dlMsgAlloc(BO report for MAC*/ 
1956    if(hqP->ulLcPrbEst.dedLcList.count != 0)
1957       updateGrantSizeForBoRpt(&(hqP->ulLcPrbEst.dedLcList), NULLP, ueCb->bsrInfo, totTBS);
1958
1959    updateGrantSizeForBoRpt(&(hqP->ulLcPrbEst.defLcList), NULLP, ueCb->bsrInfo, totTBS);
1960
1961    /*Below case will hit if NO LC(s) are allocated due to resource crunch*/
1962    if (*totTBS == 0)
1963    {
1964       if(maxFreePRB == 0)
1965       {
1966          DU_LOG("\nERROR  --> SCH : NO FREE PRB!!");
1967       }
1968       else
1969       {
1970          /*Schedule the LC for next slot*/
1971          DU_LOG("\nDEBUG  -->  SCH : No LC has been scheduled");
1972       }      
1973       return RFAILED;
1974    }   
1975    return ROK;
1976 }
1977
1978 /*******************************************************************
1979  *
1980  * @brief sch Process pending Sr or Bsr Req
1981  *
1982  * @details
1983  *
1984  *    Function : schProcessSrOrBsrReq
1985  *
1986  *    Functionality:
1987  *       sch Process pending Sr or Bsr Req
1988  *
1989  * @params[in] SchCellCb *cell,  SlotTimingInfo currTime
1990  * @params[in] uint8_t ueId, Bool isRetx, SchUlHqProcCb **hqP
1991  * @return true  - success
1992  *         false - failure
1993  *
1994  *******************************************************************/
1995 bool schProcessSrOrBsrReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId, bool isRetx, SchUlHqProcCb **hqP)
1996 {
1997    bool k2Found = FALSE;
1998    uint8_t ret = RFAILED;
1999    uint8_t startSymb = 0, symbLen = 0;
2000    uint8_t k2TblIdx = 0, k2Index = 0, k2Val = 0;
2001    uint16_t startPrb = 0;
2002    uint32_t totDataReq = 0; /* in bytes */
2003    SchUeCb *ueCb;
2004    SchPuschInfo *puschInfo;
2005    DciInfo  *dciInfo = NULLP;
2006    SchK2TimingInfoTbl *k2InfoTbl=NULLP;
2007    SlotTimingInfo dciTime, puschTime;
2008    
2009    if(cell == NULLP)
2010    {
2011       DU_LOG("\nERROR  -->  SCH: schProcessSrOrBsrReq() : Cell is NULL");
2012       return false;
2013    }
2014
2015    ueCb = &cell->ueCb[ueId-1];
2016
2017    if(ueCb == NULLP)
2018    {
2019       DU_LOG("\nERROR  -->  SCH: schProcessSrOrBsrReq() : UE is NULL");
2020       return false;
2021    }
2022
2023    if (isRetx == FALSE)
2024    {
2025       if (schUlGetAvlHqProcess(cell, ueCb, hqP) != ROK)
2026       {
2027          return RFAILED;
2028       }
2029    }
2030
2031    /* Calculating time frame to send DCI for SR */
2032    ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA, cell->numSlots);
2033 #ifdef NR_TDD
2034    if(schGetSlotSymbFrmt(dciTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
2035 #endif
2036    {     
2037       if(ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.k2TblPrsnt)
2038          k2InfoTbl = &ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.k2InfoTbl;
2039       else
2040          k2InfoTbl =  &cell->cellCfg.schInitialUlBwp.k2InfoTbl;
2041
2042       for(k2TblIdx = 0; k2TblIdx < k2InfoTbl->k2TimingInfo[dciTime.slot].numK2; k2TblIdx++)
2043       {
2044          k2Index = k2InfoTbl->k2TimingInfo[dciTime.slot].k2Indexes[k2TblIdx];
2045
2046          if(!ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.k2TblPrsnt)
2047          {
2048             k2Val = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
2049             startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
2050             symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
2051          }
2052          else
2053          {
2054             k2Val = ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].k2;
2055             startSymb =  ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].startSymbol;
2056             symbLen =  ueCb->ueCfg.spCellCfg.servCellRecfg.initUlBwp.puschCfg.timeDomRsrcAllocList[k2Index].symbolLength;
2057          }
2058          /* Check for number of Symbol of PUSCH should be same as original in case of transmisson*/
2059          /* Calculating time frame to send PUSCH for SR */
2060          ADD_DELTA_TO_TIME(dciTime, puschTime, k2Val, cell->numSlots);
2061 #ifdef NR_TDD
2062          if(schGetSlotSymbFrmt(puschTime.slot, cell->slotFrmtBitMap) == DL_SLOT)
2063             continue;
2064 #endif
2065          if(cell->schUlSlotInfo[puschTime.slot]->puschUe != 0)
2066          {
2067             continue;
2068          }
2069          k2Found = true;
2070          if(hqP)
2071          {
2072             ADD_DELTA_TO_TIME(puschTime, (*hqP)->puschTime, 0, cell->numSlots);
2073          }
2074          break;
2075       }
2076    }
2077    
2078    if(k2Found == true)
2079    {
2080       ret = schCalculateUlTbs(ueCb, puschTime, symbLen, &startPrb, &totDataReq, isRetx, *hqP);
2081    
2082       if(totDataReq > 0 && ret == ROK)
2083       {
2084          SCH_ALLOC(dciInfo, sizeof(DciInfo));
2085          if(!dciInfo)
2086          {
2087             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciInfo alloc");
2088             if(isRetx != TRUE)
2089             {
2090                if((*hqP)->ulLcPrbEst.dedLcList.count != 0)
2091                   updateBsrAndLcList(&((*hqP)->ulLcPrbEst.dedLcList), ueCb->bsrInfo, RFAILED);
2092
2093                updateBsrAndLcList(&((*hqP)->ulLcPrbEst.defLcList), ueCb->bsrInfo, RFAILED);
2094             }
2095             return false;
2096          }
2097          cell->schDlSlotInfo[dciTime.slot]->ulGrant = dciInfo;
2098          memset(dciInfo,0,sizeof(DciInfo));
2099
2100          /* Update PUSCH allocation */
2101          if(schFillPuschAlloc(ueCb, puschTime, totDataReq, startSymb, symbLen, startPrb, isRetx, *hqP) == ROK)
2102          {
2103             if(cell->schUlSlotInfo[puschTime.slot]->schPuschInfo)
2104             {
2105                puschInfo = cell->schUlSlotInfo[puschTime.slot]->schPuschInfo;
2106                if(puschInfo != NULLP)
2107                {
2108                   /* Fill DCI for UL grant */
2109                   schFillUlDci(ueCb, puschInfo, dciInfo, isRetx, *hqP);
2110                   memcpy(&dciInfo->slotIndInfo, &dciTime, sizeof(SlotTimingInfo));
2111                   ueCb->srRcvd = false;
2112                   ueCb->bsrRcvd = false;
2113                   cell->schUlSlotInfo[puschTime.slot]->puschUe = ueId;
2114                   if((*hqP)->ulLcPrbEst.dedLcList.count != 0)
2115                      updateBsrAndLcList(&((*hqP)->ulLcPrbEst.dedLcList), ueCb->bsrInfo, ROK);
2116                   updateBsrAndLcList(&((*hqP)->ulLcPrbEst.defLcList), ueCb->bsrInfo, ROK);
2117                   cmLListAdd2Tail(&(ueCb->hqUlmap[puschTime.slot]->hqList), &(*hqP)->ulSlotLnk);                  
2118                   return true;
2119                }
2120             }
2121          }
2122          if((*hqP)->ulLcPrbEst.dedLcList.count != 0)
2123             updateBsrAndLcList(&((*hqP)->ulLcPrbEst.dedLcList), ueCb->bsrInfo, RFAILED);
2124          updateBsrAndLcList(&((*hqP)->ulLcPrbEst.defLcList), ueCb->bsrInfo, RFAILED);
2125       }
2126    }
2127    else
2128    {
2129       DU_LOG("\nERROR  -->  SCH : schProcessSrOrBsrReq(): K2 value is not found");
2130       return false;     
2131    }
2132    return true;
2133 }
2134
2135
2136 /*******************************************************************
2137  *
2138  * @brief sch Process pending Sr or Bsr Req
2139  *
2140  * @details
2141  *
2142  *    Function : updateBsrAndLcList
2143  *
2144  *    Functionality:
2145  *       Updating the BSRInfo in UECB and Lclist
2146  *
2147  * @params[in] SchCellCb *cell,  SlotTimingInfo currTime 
2148  * @return ROK     - success
2149  *         RFAILED - failure
2150  *
2151  *******************************************************************/
2152 void updateBsrAndLcList(CmLListCp *lcLL, BsrInfo *bsrInfo, uint8_t status)
2153 {
2154    CmLList *node = NULLP, *next = NULLP;
2155    LcInfo *lcNode = NULLP;
2156
2157    if(lcLL == NULLP)
2158    {
2159       DU_LOG("\nERROR --> SCH: LcList not present");
2160       return;
2161    }
2162
2163    if(lcLL->count)
2164    {
2165       node = lcLL->first;
2166    }
2167    else
2168    {
2169       /*lcLL is empty*/
2170       return;
2171    }
2172
2173    while(node)
2174    {
2175       next = node->next;
2176       lcNode = (LcInfo *)node->node;
2177       if(lcNode != NULLP)
2178       {
2179           /*Only when Status is OK then allocation is marked as ZERO and reqBO
2180            * is updated in UE's DB. If Failure, then allocation is added to reqBO 
2181            * and same is updated in Ue's DB inside BSR Info structure*/
2182          if(status == ROK)
2183          {
2184             lcNode->allocBO = 0;
2185          }
2186
2187          lcNode->reqBO += lcNode->allocBO;
2188          bsrInfo[lcNode->lcId].dataVol = lcNode->reqBO;
2189          if(lcNode->reqBO == 0)
2190          {
2191             handleLcLList(lcLL, lcNode->lcId, DELETE);
2192          }
2193       }
2194       node = next;
2195    }
2196 }
2197
2198 /********************************************************************************
2199  *
2200  * @brief Increment the Slot by a input factor
2201  *
2202  * @details
2203  *
2204  *    Function : schIncrSlot
2205  *
2206  *    Functionality:
2207  *       Increment the slot by a input factor till num of Slots configured in a
2208  *       Radio Frame. If it exceeds, move to next sfn.
2209  *
2210  * @params[in/out] SlotTimingInfo timingInfo
2211  *        [in]     uint8_t incr [Increment factor]
2212  *        [in]     numSlotsPerRF [Number of Slots configured per RF as per
2213  *                                numerology]
2214  * @return ROK     - success
2215  *         RFAILED - failure
2216  *
2217  *******************************************************************/
2218 void schIncrSlot(SlotTimingInfo *timingInfo, uint8_t incr, uint16_t numSlotsPerRF)
2219 {
2220    timingInfo->slot += incr;
2221    if(timingInfo->slot >= numSlotsPerRF)
2222    {
2223       timingInfo->sfn += timingInfo->slot/numSlotsPerRF;
2224       timingInfo->slot %= numSlotsPerRF;
2225       if(timingInfo->sfn >  MAX_SFN)
2226       {
2227          timingInfo->sfn %= MAX_SFN;
2228       }
2229    }
2230 }
2231
2232 /*******************************************************************
2233 *
2234 * @brief   Fill PDSCH info in Page Alloc
2235 *
2236 * @details
2237 *
2238 *    Function : schFillPagePdschCfg 
2239 *
2240 *    Functionality: Fill PDSCH info in Page Alloc
2241 *
2242 * @params[in] SchCellCb *cell, PdschCfg *pagePdschCfg, SlotTimingInfo slotTime, 
2243 *             uint16_t tbsSize, uint8_t mcs, uint16_t startPrb
2244 *
2245 * @return pointer to return Value(ROK, RFAILED) 
2246 *
2247 * ****************************************************************/
2248 uint8_t schFillPagePdschCfg(SchCellCb *cell, PdschCfg *pagePdschCfg, SlotTimingInfo slotTime, uint16_t tbSize, uint8_t mcs, uint16_t startPrb)
2249 {
2250    uint8_t cwCount = 0;
2251    uint8_t dmrsStartSymbol, startSymbol, numSymbol;
2252
2253    /* fill the PDSCH PDU */
2254
2255    pagePdschCfg->pduBitmap = 0; /* PTRS and CBG params are excluded */
2256    pagePdschCfg->rnti = P_RNTI; /* SI-RNTI */
2257    pagePdschCfg->pduIndex = 0;
2258    pagePdschCfg->numCodewords = 1;
2259    for(cwCount = 0; cwCount < pagePdschCfg->numCodewords; cwCount++)
2260    {
2261       pagePdschCfg->codeword[cwCount].targetCodeRate = 308;
2262       pagePdschCfg->codeword[cwCount].qamModOrder = 2;
2263       pagePdschCfg->codeword[cwCount].mcsIndex = mcs;
2264       pagePdschCfg->codeword[cwCount].mcsTable = 0; /* notqam256 */
2265       pagePdschCfg->codeword[cwCount].rvIndex = 0;
2266       tbSize = tbSize + TX_PAYLOAD_HDR_LEN;
2267       pagePdschCfg->codeword[cwCount].tbSize = tbSize;
2268    }
2269    pagePdschCfg->dataScramblingId                   = cell->cellCfg.phyCellId;
2270    pagePdschCfg->numLayers                          = 1;
2271    pagePdschCfg->transmissionScheme                 = 0;
2272    pagePdschCfg->refPoint                           = 0;
2273    pagePdschCfg->dmrs.dlDmrsSymbPos                 = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
2274    pagePdschCfg->dmrs.dmrsConfigType                = 0; /* type-1 */
2275    pagePdschCfg->dmrs.dlDmrsScramblingId            = cell->cellCfg.phyCellId;
2276    pagePdschCfg->dmrs.scid                          = 0;
2277    pagePdschCfg->dmrs.numDmrsCdmGrpsNoData          = 1;
2278    pagePdschCfg->dmrs.dmrsPorts                     = 0x0001;
2279    pagePdschCfg->dmrs.mappingType                   = DMRS_MAP_TYPE_A; /* Type-A */
2280    pagePdschCfg->dmrs.nrOfDmrsSymbols               = NUM_DMRS_SYMBOLS;
2281    pagePdschCfg->dmrs.dmrsAddPos                    = DMRS_ADDITIONAL_POS;
2282
2283    pagePdschCfg->pdschFreqAlloc.resourceAllocType   = 1; /* RAT type-1 RIV format */
2284    /* the RB numbering starts from coreset0, and PDSCH is always above SSB */
2285    pagePdschCfg->pdschFreqAlloc.freqAlloc.startPrb  = startPrb;
2286    pagePdschCfg->pdschFreqAlloc.freqAlloc.numPrb    = schCalcNumPrb(tbSize, mcs, NUM_PDSCH_SYMBOL);
2287    pagePdschCfg->pdschFreqAlloc.vrbPrbMapping       = 0; /* non-interleaved */
2288    pagePdschCfg->pdschTimeAlloc.rowIndex            = 1;
2289    /* This is Intel's requirement. PDSCH should start after PDSCH DRMS symbol */
2290    pagePdschCfg->pdschTimeAlloc.timeAlloc.startSymb = 3; /* spec-38.214, Table 5.1.2.1-1 */
2291    pagePdschCfg->pdschTimeAlloc.timeAlloc.numSymb   = NUM_PDSCH_SYMBOL;
2292
2293    /* Find total symbols occupied including DMRS */
2294    dmrsStartSymbol = findDmrsStartSymbol(pagePdschCfg->dmrs.dlDmrsSymbPos);
2295    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT,
2296     * in that case only PDSCH symbols are marked as occupied */
2297    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
2298    {
2299       startSymbol = pagePdschCfg->pdschTimeAlloc.timeAlloc.startSymb;
2300       numSymbol = pagePdschCfg->pdschTimeAlloc.timeAlloc.numSymb;
2301    }
2302    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
2303    else
2304    {
2305       startSymbol = dmrsStartSymbol;
2306       numSymbol = pagePdschCfg->dmrs.nrOfDmrsSymbols + pagePdschCfg->pdschTimeAlloc.timeAlloc.numSymb;
2307    }
2308
2309    /* Allocate the number of PRBs required for DL PDSCH */
2310    if((allocatePrbDl(cell, slotTime, startSymbol, numSymbol,\
2311                &pagePdschCfg->pdschFreqAlloc.freqAlloc.startPrb, pagePdschCfg->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
2312    {
2313       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for DL MSG");
2314       return RFAILED;
2315    }
2316
2317    pagePdschCfg->beamPdschInfo.numPrgs              = 1;
2318    pagePdschCfg->beamPdschInfo.prgSize              = 1;
2319    pagePdschCfg->beamPdschInfo.digBfInterfaces      = 0;
2320    pagePdschCfg->beamPdschInfo.prg[0].pmIdx         = 0;
2321    pagePdschCfg->beamPdschInfo.prg[0].beamIdx[0]    = 0;
2322    pagePdschCfg->txPdschPower.powerControlOffset    = 0;
2323    pagePdschCfg->txPdschPower.powerControlOffsetSS  = 0;
2324
2325    return ROK;
2326 }
2327
2328 /**********************************************************************
2329   End of file
2330  **********************************************************************/
2331