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