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