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