JIRA ID: ODUHIGH-381 : DL packets Scheduling as per RRM Policy
[o-du/l2.git] / src / 5gnrsch / sch.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:     sch.c
22   
23      Type:     C source file
24   
25      Desc:     C source code for scheduler fucntions
26   
27      File:     sch.c
28   
29 **********************************************************************/
30
31 /** @file sch.c
32 @brief This file implements the schedulers main access to MAC layer code.
33 */
34 #include "common_def.h" 
35 #include "du_app_mac_inf.h"
36 #include "lrg.h"
37 #include "tfu.h"
38 #include "du_log.h"
39 #include "rgr.h"
40 #include "rg_sch_inf.h"
41 #include "rg_sch.h"
42
43 #include "tfu.x"           /* TFU types */
44 #include "lrg.x"           /* layer management typedefs for MAC */
45 #include "rgr.x"           /* layer management typedefs for MAC */
46 #include "rg_sch_inf.x"         /* typedefs for Scheduler */
47 #include "mac_sch_interface.h"
48 #include "sch.h"
49 #include "sch_utils.h"
50
51 void SchFillCfmPst(Pst *reqPst,Pst *cfmPst,RgMngmt *cfm);
52
53 /* local defines */
54 SchCellCfgCfmFunc SchCellCfgCfmOpts[] = 
55 {
56    packSchCellCfgCfm,     /* LC */
57    MacProcSchCellCfgCfm,  /* TC */
58    packSchCellCfgCfm      /* LWLC */
59 };
60
61
62 /**
63  * @brief Task Initiation function. 
64  *
65  * @details
66  *
67  *     Function : schActvInit
68  *     
69  *     This function is supplied as one of parameters during MAC's 
70  *     task registration. MAC will invoke this function once, after
71  *     it creates and attaches this TAPA Task to a system task.
72  *     
73  *  @param[in]  Ent Entity, the entity ID of this task.     
74  *  @param[in]  Inst Inst, the instance ID of this task.
75  *  @param[in]  Region Region, the region ID registered for memory 
76  *              usage of this task.
77  *  @param[in]  Reason Reason.
78  *  @return  int
79  *      -# ROK
80  **/
81 uint8_t schActvInit(Ent entity, Inst instId, Region region, Reason reason)
82 {
83    Inst inst = (instId  - SCH_INST_START);
84
85    /* Initialize the MAC TskInit structure to zero */
86    memset ((uint8_t *)&schCb[inst], 0, sizeof(schCb));
87
88    /* Initialize the MAC TskInit with received values */
89    schCb[inst].schInit.ent = entity;
90    schCb[inst].schInit.inst = inst;
91    schCb[inst].schInit.region = region;
92    schCb[inst].schInit.pool = 0;
93    schCb[inst].schInit.reason = reason;
94    schCb[inst].schInit.cfgDone = FALSE;
95    schCb[inst].schInit.acnt = FALSE;
96    schCb[inst].schInit.usta = FALSE;
97    schCb[inst].schInit.trc = FALSE;
98    schCb[inst].schInit.procId = ODU_GET_PROCID();
99
100    return ROK;
101 } /* schActvInit */
102
103 /**
104  * @brief Scheduler instance Configuration Handler. 
105  *
106  * @details
107  *
108  *     Function : SchInstCfg
109  *     
110  *     This function in called by SchProcGenCfgReq(). It handles the
111  *     general configurations of the scheduler instance. Returns
112  *     reason for success/failure of this function.
113  *     
114  *  @param[in]  RgCfg *cfg, the Configuaration information 
115  *  @return  uint16_t
116  *      -# LCM_REASON_NOT_APPL 
117  *      -# LCM_REASON_INVALID_MSGTYPE
118  *      -# LCM_REASON_MEM_NOAVAIL
119  **/
120 uint8_t SchInstCfg(RgCfg *cfg, Inst  dInst)
121 {
122    uint16_t ret = LCM_REASON_NOT_APPL;
123    Inst     inst = (dInst - SCH_INST_START);
124
125    DU_LOG("\nDEBUG  -->  SCH : Entered SchInstCfg()");
126    /* Check if Instance Configuration is done already */
127    if (schCb[inst].schInit.cfgDone == TRUE)
128    {
129       return LCM_REASON_INVALID_MSGTYPE;
130    }
131    /* Update the Pst structure for LM interface */
132    memcpy(&schCb[inst].schInit.lmPst,
133          &cfg->s.schInstCfg.genCfg.lmPst,
134          sizeof(Pst));
135
136    schCb[inst].schInit.inst = inst;
137    schCb[inst].schInit.lmPst.srcProcId = schCb[inst].schInit.procId;
138    schCb[inst].schInit.lmPst.srcEnt = schCb[inst].schInit.ent;
139    schCb[inst].schInit.lmPst.srcInst = schCb[inst].schInit.inst +
140       SCH_INST_START;
141    schCb[inst].schInit.lmPst.event = EVTNONE;
142
143    schCb[inst].schInit.region = cfg->s.schInstCfg.genCfg.mem.region;
144    schCb[inst].schInit.pool = cfg->s.schInstCfg.genCfg.mem.pool;
145    schCb[inst].genCfg.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
146 #ifdef LTE_ADV
147    schCb[inst].genCfg.forceCntrlSrbBoOnPCel =  cfg->s.schInstCfg.genCfg.forceCntrlSrbBoOnPCel;
148    schCb[inst].genCfg.isSCellActDeactAlgoEnable =  cfg->s.schInstCfg.genCfg.isSCellActDeactAlgoEnable;
149 #endif
150    schCb[inst].genCfg.startCellId    = cfg->s.schInstCfg.genCfg.startCellId;
151
152    /* Initialzie the timer queue */   
153    memset(&schCb[inst].tmrTq, 0, sizeof(CmTqType) * SCH_TQ_SIZE);
154    /* Initialize the timer control point */
155    memset(&schCb[inst].tmrTqCp, 0, sizeof(CmTqCp));
156    schCb[inst].tmrTqCp.tmrLen = RGSCH_TQ_SIZE;
157
158    /* SS_MT_TMR needs to be enabled as schActvTmr needs instance information */
159    /* Timer Registration request to system services */
160    if (ODU_REG_TMR_MT(schCb[inst].schInit.ent, dInst,
161             (int)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
162    {
163       DU_LOG("\nERROR  -->  SCH : SchInstCfg(): Failed to "
164             "register timer.");
165       return (LCM_REASON_MEM_NOAVAIL);
166    }   
167               
168    /* Set Config done in TskInit */
169    schCb[inst].schInit.cfgDone = TRUE;
170    DU_LOG("\nINFO  -->  SCH : Scheduler gen config done");
171
172    return ret;
173 }
174
175 /**
176  * @brief Layer Manager Configuration request handler. 
177  *
178  * @details
179  *
180  *     Function : SchProcGenCfgReq
181  *     
182  *     This function handles the configuration
183  *     request received at scheduler instance from the Layer Manager.
184  *     -# Based on the cfg->hdr.elmId.elmnt value it invokes one of the
185  *        functions rgHdlGenCfg() or rgHdlSapCfg().
186  *     -# Invokes RgMiLrgSchCfgCfm() to send back the confirmation to the LM.
187  *     
188  *  @param[in]  Pst *pst, the post structure     
189  *  @param[in]  RgMngmt *cfg, the configuration parameter's structure
190  *  @return  S16
191  *      -# ROK
192  **/
193 uint8_t SchProcGenCfgReq(Pst *pst, RgMngmt *cfg)
194 {
195    uint8_t   ret = LCM_PRIM_OK;
196    uint16_t  reason = LCM_REASON_NOT_APPL;
197    RgMngmt   cfm;
198    Pst       cfmPst;
199
200 #ifdef CALL_FLOW_DEBUG_LOG
201    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : GENERAL_CFG_REQ\n");
202 #endif
203
204    if(pst->dstInst < SCH_INST_START)
205    {
206       DU_LOG("\nERROR  -->  SCH : Invalid inst ID");
207       DU_LOG("\nERROR  -->  SCH : SchProcGenCfgReq(): "
208             "pst->dstInst=%d SCH_INST_START=%d", pst->dstInst,SCH_INST_START); 
209       return ROK;
210    }
211    DU_LOG("\nINFO -->  SCH : Received scheduler gen config");
212    /* Fill the post structure for sending the confirmation */
213    memset(&cfmPst, 0 , sizeof(Pst));
214    SchFillCfmPst(pst, &cfmPst, cfg);
215
216    memset(&cfm, 0, sizeof(RgMngmt));
217
218 #ifdef LMINT3
219    cfm.hdr.transId =
220       cfg->hdr.transId;
221 #endif
222
223    cfm.hdr.elmId.elmnt = cfg->hdr.elmId.elmnt;
224    switch(cfg->hdr.elmId.elmnt)
225    {
226       case STSCHINST:
227          reason = SchInstCfg(&cfg->t.cfg,pst->dstInst );
228          break;
229       default:
230          ret = LCM_PRIM_NOK;
231          reason = LCM_REASON_INVALID_ELMNT;
232          DU_LOG("\nERROR  -->  SCH : Invalid Elmnt=%d", cfg->hdr.elmId.elmnt);
233          break;
234    }
235
236    if (reason != LCM_REASON_NOT_APPL)
237    {
238       ret = LCM_PRIM_NOK;
239    }
240
241    cfm.cfm.status = ret;
242    cfm.cfm.reason = reason;
243
244    SchSendCfgCfm(&cfmPst, &cfm);
245    /*   SCH_FREE(pst->region, pst->pool, (Data *)cfg, sizeof(RgMngmt)); */
246
247    return ROK;
248 }/*-- SchProcGenCfgReq --*/
249
250 /**
251  * @brief slot indication from MAC to SCH.
252  *
253  * @details
254  *
255  *     Function : MacSchSlotInd 
256  *      
257  *      This API is invoked by PHY to indicate slot indication to Scheduler for
258  *      a cell.
259  *           
260  *  @param[in]  Pst            *pst
261  *  @param[in]  SlotTimingInfo    *slotInd
262  *  @return  S16
263  *      -# ROK 
264  *      -# RFAILED 
265  **/
266 uint8_t MacSchSlotInd(Pst *pst, SlotTimingInfo *slotInd)
267 {
268    Inst  inst = pst->dstInst-SCH_INST_START;
269
270 #ifdef CALL_FLOW_DEBUG_LOG
271    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_SLOT_IND_TO_SCH\n");
272 #endif
273
274    schProcessSlotInd(slotInd, inst);
275
276    return ROK;
277 }  /* MacSchSlotInd */
278
279 /*******************************************************************
280  *
281  * @brief Processes Rach indication from MAC 
282  *
283  * @details
284  *
285  *    Function : MacSchRachInd
286  *
287  *    Functionality:
288  *      Processes Rach indication from MAC
289  *
290  * @params[in] 
291  * @return ROK     - success
292  *         RFAILED - failure
293  *
294  * ****************************************************************/
295 uint8_t MacSchRachInd(Pst *pst, RachIndInfo *rachInd)
296 {
297    Inst  inst = pst->dstInst-SCH_INST_START;
298
299 #ifdef CALL_FLOW_DEBUG_LOG
300    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_RACH_IND_TO_SCH\n");
301 #endif
302
303    DU_LOG("\nINFO  -->  SCH : Received Rach indication");
304    schProcessRachInd(rachInd, inst);
305    return ROK;
306 }
307
308 /*******************************************************************
309  *
310  * @brief Processes CRC indication from MAC 
311  *
312  * @details
313  *
314  *    Function : MacSchCrcInd
315  *
316  *    Functionality:
317  *      Processes CRC indication from MAC
318  *
319  * @params[in] Post structure
320  *             Crc Indication
321  * @return ROK     - success
322  *         RFAILED - failure
323  *
324  * ****************************************************************/
325 uint8_t MacSchCrcInd(Pst *pst, CrcIndInfo *crcInd)
326 {
327 #ifdef CALL_FLOW_DEBUG_LOG
328    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_CRC_IND_TO_SCH\n");
329 #endif
330
331    switch(crcInd->crcInd[0])
332    {
333       case CRC_FAILED:
334          DU_LOG("\nDEBUG  -->  SCH : Received CRC indication. CRC Status [FAILURE]");
335          break;
336       case CRC_PASSED:
337          DU_LOG("\nDEBUG  -->  SCH : Received CRC indication. CRC Status [PASS]");
338          break;
339       default:
340          DU_LOG("\nDEBUG  -->  SCH : Invalid CRC state %d", crcInd->crcInd[0]);
341          return RFAILED;
342    }
343    return ROK;
344 }
345
346 #ifdef NR_TDD
347 /**
348  *@brief Returns TDD periodicity in micro seconds
349  *
350  * @details
351  * 
352  * Function : schGetPeriodicityInMsec 
353  * 
354  * This API retunrs TDD periodicity in micro seconds
355  * 
356  * @param[in] DlUlTxPeriodicity 
357  * @return  periodicityInMsec
358  * **/
359
360 uint16_t schGetPeriodicityInMsec(DlUlTxPeriodicity tddPeriod)
361 {
362    uint16_t  periodicityInMsec = 0;
363    switch(tddPeriod)
364    {
365       case TX_PRDCTY_MS_0P5:
366       {
367          periodicityInMsec = 500;
368          break;
369       }
370       case TX_PRDCTY_MS_0P625:
371       {
372          periodicityInMsec = 625;
373          break;
374       }
375       case TX_PRDCTY_MS_1:
376       {
377          periodicityInMsec = 1000;
378          break;
379       }
380       case TX_PRDCTY_MS_1P25:
381       {
382          periodicityInMsec = 1250;
383          break;
384       }
385       case TX_PRDCTY_MS_2:
386       {
387          periodicityInMsec = 2000;
388          break;
389       }
390       case TX_PRDCTY_MS_2P5:
391       {
392          periodicityInMsec = 2500;
393          break;
394       }
395       case TX_PRDCTY_MS_5:
396       {
397          periodicityInMsec = 5000;
398          break;
399       }
400       case TX_PRDCTY_MS_10:
401       {
402          periodicityInMsec = 10000;
403          break;
404       }
405       default:
406       {
407          DU_LOG("\nERROR  -->  SCH : Invalid DlUlTxPeriodicity:%d", tddPeriod);
408       }
409    }
410
411    return periodicityInMsec;
412 }
413
414
415 /**
416  * @brief init TDD slot config 
417  *
418  * @details
419  *
420  *     Function : schInitTddSlotCfg 
421  *      
422  *      This API is invoked after receiving schCellCfg
423  *           
424  *  @param[in]  schCellCb *cell
425  *  @param[in]  SchCellCfg *schCellCfg
426  *  @return  void
427  **/
428 void schInitTddSlotCfg(SchCellCb *cell, SchCellCfg *schCellCfg)
429 {
430    uint16_t periodicityInMicroSec = 0;
431    int8_t slotIdx, symbIdx;
432
433    periodicityInMicroSec = schGetPeriodicityInMsec(schCellCfg->tddCfg.tddPeriod);
434    cell->numSlotsInPeriodicity = (periodicityInMicroSec * pow(2, schCellCfg->numerology))/1000;
435    cell->slotFrmtBitMap = 0;
436    cell->symbFrmtBitMap = 0;
437    for(slotIdx = cell->numSlotsInPeriodicity-1; slotIdx >= 0; slotIdx--)
438    {
439       symbIdx = 0;
440       /* If the first and last symbol are the same, the entire slot is the same type */
441       if((schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx] == schCellCfg->tddCfg.slotCfg[slotIdx][MAX_SYMB_PER_SLOT-1]) &&
442               schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx] != FLEXI_SLOT)
443       {
444          switch(schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx])
445          {
446             case DL_SLOT:
447             {
448                /*BitMap to be set to 00 */
449                cell->slotFrmtBitMap = (cell->slotFrmtBitMap<<2);
450                break;
451             }
452             case UL_SLOT:
453             {
454                /*BitMap to be set to 01 */
455                cell->slotFrmtBitMap = ((cell->slotFrmtBitMap<<2) | (UL_SLOT));
456                break;
457             }
458             default:
459                DU_LOG("\nERROR  -->  SCH : Invalid slot Config in schInitTddSlotCfg");
460            }
461          continue;
462       }
463       /* slot config is flexible. First set slotBitMap to 10 */
464       cell->slotFrmtBitMap = ((cell->slotFrmtBitMap<<2) | (FLEXI_SLOT));
465
466       /* Now set symbol bitmap */ 
467       for(symbIdx = MAX_SYMB_PER_SLOT-1; symbIdx >= 0; symbIdx--)
468       {
469          switch(schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx])
470          {
471             case DL_SLOT:
472             {
473                /*symbol BitMap to be set to 00 */
474                cell->symbFrmtBitMap = (cell->symbFrmtBitMap<<2);
475                break;
476             }
477             case UL_SLOT:
478             {
479                /*symbol BitMap to be set to 01 */
480                cell->symbFrmtBitMap = ((cell->symbFrmtBitMap<<2) | (UL_SLOT));
481                break;
482             }
483             case FLEXI_SLOT:
484             {
485                /*symbol BitMap to be set to 10 */
486                cell->symbFrmtBitMap = ((cell->symbFrmtBitMap<<2) | (FLEXI_SLOT));
487                break;
488             }
489             default:
490                DU_LOG("\nERROR  -->  SCH : Invalid slot Config in schInitTddSlotCfg");
491          }
492       }
493    }
494 }
495 #endif
496
497 /**
498  * @brief Fill SSB start symbol
499  *
500  * @details
501  *
502  *     Function : fillSsbStartSymb 
503  *      
504  *      This API stores SSB start index per beam
505  *           
506  *  @param[in]  SchCellCb     *cellCb
507  *  @return  int
508  *      -# ROK 
509  *      -# RFAILED 
510  **/
511 void fillSsbStartSymb(SchCellCb *cellCb)
512 {
513    uint8_t cnt, scs, symbIdx, ssbStartSymbArr[SCH_MAX_SSB_BEAM];
514
515    scs = cellCb->cellCfg.ssbSchCfg.scsCommon;
516
517    memset(ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
518    symbIdx = 0;
519    /* Determine value of "n" based on Section 4.1 of 3GPP TS 38.213 */
520    switch(scs)
521    {
522       case SCS_15KHZ:
523          {
524             if(cellCb->cellCfg.dlFreq <= 300000)
525                cnt = 2;/* n = 0, 1 */
526             else
527                cnt = 4; /* n = 0, 1, 2, 3 */
528             for(uint8_t idx=0; idx<cnt; idx++)
529             {
530                /* start symbol determined using {2, 8} + 14n */
531                ssbStartSymbArr[symbIdx++] = 2 +  MAX_SYMB_PER_SLOT*idx;
532                ssbStartSymbArr[symbIdx++] = 8 +  MAX_SYMB_PER_SLOT*idx;
533             }
534          }
535          break;
536       case SCS_30KHZ:
537          {
538             if(cellCb->cellCfg.dlFreq <= 300000)
539                cnt = 1;/* n = 0 */
540             else
541                cnt = 2; /* n = 0, 1 */
542             for(uint8_t idx=0; idx<cnt; idx++)
543             {
544                /* start symbol determined using {4, 8, 16, 20} + 28n */
545                ssbStartSymbArr[symbIdx++] = 4 +  MAX_SYMB_PER_SLOT*idx;
546                ssbStartSymbArr[symbIdx++] = 8 +  MAX_SYMB_PER_SLOT*idx;
547                ssbStartSymbArr[symbIdx++] = 16 +  MAX_SYMB_PER_SLOT*idx;
548                ssbStartSymbArr[symbIdx++] = 20 +  MAX_SYMB_PER_SLOT*idx;
549             }
550          }
551          break;
552       default:
553          DU_LOG("\nERROR  -->  SCH : SCS %d is currently not supported", scs);
554    }
555    memset(cellCb->ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
556    memcpy(cellCb->ssbStartSymbArr, ssbStartSymbArr, SCH_MAX_SSB_BEAM);
557
558 }
559
560
561 /**
562  * @brief init cellCb based on cellCfg
563  *
564  * @details
565  *
566  *     Function : schInitCellCb 
567  *      
568  *      This API is invoked after receiving schCellCfg
569  *           
570  *  @param[in]  schCellCb *cell
571  *  @param[in]  SchCellCfg *schCellCfg
572  *  @return  int
573  *      -# ROK 
574  *      -# RFAILED 
575  **/
576 uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
577 {
578    SchCellCb *cell= NULLP;
579    SCH_ALLOC(cell, sizeof(SchCellCb));
580    if(!cell)
581    {
582       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
583       return RFAILED;
584    }
585
586    cell->cellId = schCellCfg->cellId; 
587    cell->instIdx = inst;
588    switch(schCellCfg->numerology)
589    {
590       case SCH_NUMEROLOGY_0:
591          {
592             cell->numSlots = SCH_MU0_NUM_SLOTS;
593          }
594          break;
595       case SCH_NUMEROLOGY_1:
596          {
597             cell->numSlots = SCH_MU1_NUM_SLOTS;
598          }
599          break;
600       case SCH_NUMEROLOGY_2:
601          {
602             cell->numSlots = SCH_MU2_NUM_SLOTS;
603          }
604          break;
605       case SCH_NUMEROLOGY_3:
606          {
607             cell->numSlots = SCH_MU3_NUM_SLOTS;
608          }
609          break;
610       case SCH_NUMEROLOGY_4:
611          {
612             cell->numSlots = SCH_MU4_NUM_SLOTS;
613          }
614          break;
615       default:
616          DU_LOG("\nERROR  -->  SCH : Numerology %d not supported", schCellCfg->numerology);
617    }
618 #ifdef NR_TDD
619    schInitTddSlotCfg(cell, schCellCfg);   
620 #endif
621
622    SCH_ALLOC(cell->schDlSlotInfo, cell->numSlots * sizeof(SchDlSlotInfo*));
623    if(!cell->schDlSlotInfo)
624    {
625       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schDlSlotInfo");
626       return RFAILED;
627    }
628
629    SCH_ALLOC(cell->schUlSlotInfo, cell->numSlots * sizeof(SchUlSlotInfo*));
630    if(!cell->schUlSlotInfo)
631    {
632       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schUlSlotInfo");
633       return RFAILED;
634    }
635
636    for(uint8_t idx=0; idx<cell->numSlots; idx++)
637    {
638       SchDlSlotInfo *schDlSlotInfo;
639       SchUlSlotInfo *schUlSlotInfo;
640
641       /* DL Alloc */
642       SCH_ALLOC(schDlSlotInfo, sizeof(SchDlSlotInfo));
643       if(!schDlSlotInfo)
644       {
645          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
646          return RFAILED;
647       }
648
649       /* UL Alloc */
650       SCH_ALLOC(schUlSlotInfo, sizeof(SchUlSlotInfo));
651       if(!schUlSlotInfo)
652       {
653          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
654          return RFAILED;
655       }
656
657       schInitDlSlot(schDlSlotInfo);
658       schInitUlSlot(schUlSlotInfo);
659
660       cell->schDlSlotInfo[idx] = schDlSlotInfo;
661       cell->schUlSlotInfo[idx] = schUlSlotInfo;
662
663    }
664    cell->firstSsbTransmitted = false;
665    cell->firstSib1Transmitted = false;
666    fillSsbStartSymb(cell);
667    schCb[inst].cells[inst] = cell;
668
669    DU_LOG("\nINFO  -->  SCH : Cell init completed for cellId:%d", cell->cellId);
670
671    return ROK;   
672 }
673
674 /**
675  * @brief Fill SIB1 configuration
676  *
677  * @details
678  *
679  *     Function : fillSchSib1Cfg
680  *
681  *     Fill SIB1 configuration
682  *
683  *  @param[in]  uint8_t bandwidth : total available bandwidth
684  *              uint8_t numSlots : total slots per SFN
685  *              SchSib1Cfg *sib1SchCfg : cfg to be filled
686  *              uint16_t pci : physical cell Id
687  *              uint8_t offsetPointA : offset
688  *  @return  void
689  **/
690 void fillSchSib1Cfg(uint8_t mu, uint8_t bandwidth, uint8_t numSlots, SchSib1Cfg *sib1SchCfg, uint16_t pci, uint8_t offsetPointA)
691 {
692    uint8_t coreset0Idx = 0;
693    uint8_t searchSpace0Idx = 0;
694    //uint8_t ssbMuxPattern = 0;
695    uint8_t numRbs = 0;
696    uint8_t numSymbols = 0;
697    uint8_t offset = 0;
698    uint8_t oValue = 0;
699    //uint8_t numSearchSpacePerSlot = 0;
700    uint8_t mValue = 0;
701    uint8_t firstSymbol = 0; /* need to calculate using formula mentioned in 38.213 */
702    uint8_t slotIndex = 0;
703    uint8_t FreqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
704    uint16_t tbSize = 0;
705    uint8_t ssbIdx = 0;
706
707    PdcchCfg *pdcch = &(sib1SchCfg->sib1PdcchCfg);
708    PdschCfg *pdsch = &(sib1SchCfg->sib1PdschCfg);
709    BwpCfg *bwp = &(sib1SchCfg->bwp);
710
711    coreset0Idx     = sib1SchCfg->coresetZeroIndex;
712    searchSpace0Idx = sib1SchCfg->searchSpaceZeroIndex;
713
714    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
715    //ssbMuxPattern = coresetIdxTable[coreset0Idx][0];
716    numRbs        = coresetIdxTable[coreset0Idx][1];
717    numSymbols    = coresetIdxTable[coreset0Idx][2];
718    offset        = coresetIdxTable[coreset0Idx][3];
719
720    /* derive the search space params from table 13-11 spec 38.213 */
721    oValue                = searchSpaceIdxTable[searchSpace0Idx][0];
722    //numSearchSpacePerSlot = searchSpaceIdxTable[searchSpace0Idx][1];
723    mValue                = searchSpaceIdxTable[searchSpace0Idx][2];
724    firstSymbol           = searchSpaceIdxTable[searchSpace0Idx][3];
725
726    /* calculate the n0, need to add the formulae, as of now the value is 0 
727     * Need to add the even and odd values of i during configuration 
728     * [(O . 2^u + i . M )  ] mod numSlotsPerSubframe 
729     * assuming u = 0, i = 0, numSlotsPerSubframe = 10
730     * Also, from this configuration, coreset0 is only on even subframe */
731    slotIndex = (int)((oValue*pow(2, mu)) + floor(ssbIdx*mValue))%numSlots;
732    sib1SchCfg->n0 = slotIndex;
733
734    /* fill BWP */
735    switch(bandwidth)
736    {
737       case BANDWIDTH_20MHZ:
738          {
739             bwp->freqAlloc.numPrb = TOTAL_PRB_20MHZ_MU0;
740          }
741          break;
742       case BANDWIDTH_100MHZ:
743          {
744             bwp->freqAlloc.numPrb = TOTAL_PRB_100MHZ_MU1;
745          }
746          break;
747       default:
748          DU_LOG("\nERROR  -->  SCH : Bandwidth %d not supported", bandwidth);
749
750    }
751    bwp->freqAlloc.startPrb = 0;
752    bwp->subcarrierSpacing  = 0;         /* 15Khz */
753    bwp->cyclicPrefix       = 0;              /* normal */
754
755    /* fill the PDCCH PDU */
756    pdcch->coresetCfg.coreSetSize = numRbs;
757    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
758    pdcch->coresetCfg.durationSymbols = numSymbols;
759    
760    /* Fill Bitmap for PRBs in coreset */
761    fillCoresetFeqDomAllocMap(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
762    covertFreqDomRsrcMapToIAPIFormat(FreqDomainResource, pdcch->coresetCfg.freqDomainResource);
763
764    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
765    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
766    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
767    pdcch->coresetCfg.coreSetType = 0;
768    pdcch->coresetCfg.shiftIndex = pci;
769    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
770    pdcch->numDlDci = 1;
771    pdcch->dci.rnti = SI_RNTI;
772    pdcch->dci.scramblingId = pci;
773    pdcch->dci.scramblingRnti = 0;
774    pdcch->dci.cceIndex = 0;
775    pdcch->dci.aggregLevel = 4;
776    pdcch->dci.beamPdcchInfo.numPrgs = 1;
777    pdcch->dci.beamPdcchInfo.prgSize = 1;
778    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
779    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
780    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
781    pdcch->dci.txPdcchPower.powerValue = 0;
782    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
783    /* Storing pdschCfg pointer here. Required to access pdsch config while
784       fillig up pdcch pdu */
785    pdcch->dci.pdschCfg = pdsch; 
786
787    /* fill the PDSCH PDU */
788    uint8_t cwCount = 0;
789    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
790    pdsch->rnti = 0xFFFF; /* SI-RNTI */
791    pdsch->pduIndex = 0;
792    pdsch->numCodewords = 1;
793    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
794    {
795       pdsch->codeword[cwCount].targetCodeRate = 308;
796       pdsch->codeword[cwCount].qamModOrder = 2;
797       pdsch->codeword[cwCount].mcsIndex = sib1SchCfg->sib1Mcs;
798       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
799       pdsch->codeword[cwCount].rvIndex = 0;
800       tbSize = schCalcTbSize(sib1SchCfg->sib1PduLen + TX_PAYLOAD_HDR_LEN);
801       pdsch->codeword[cwCount].tbSize = tbSize;
802    }
803    pdsch->dataScramblingId                   = pci;
804    pdsch->numLayers                          = 1;
805    pdsch->transmissionScheme                 = 0;
806    pdsch->refPoint                           = 0;
807    pdsch->dmrs.dlDmrsSymbPos                 = 4; /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
808    pdsch->dmrs.dmrsConfigType                = 0; /* type-1 */
809    pdsch->dmrs.dlDmrsScramblingId            = pci;
810    pdsch->dmrs.scid                          = 0;
811    pdsch->dmrs.numDmrsCdmGrpsNoData          = 1;
812    pdsch->dmrs.dmrsPorts                     = 0x0001;
813    pdsch->dmrs.mappingType                   = DMRS_MAP_TYPE_A; /* Type-A */
814    pdsch->dmrs.nrOfDmrsSymbols               = NUM_DMRS_SYMBOLS;
815    pdsch->dmrs.dmrsAddPos                    = DMRS_ADDITIONAL_POS;
816
817    pdsch->pdschFreqAlloc.resourceAllocType   = 1; /* RAT type-1 RIV format */
818    /* the RB numbering starts from coreset0, and PDSCH is always above SSB */
819    pdsch->pdschFreqAlloc.freqAlloc.startPrb  = offsetPointA + SCH_SSB_NUM_PRB;
820    pdsch->pdschFreqAlloc.freqAlloc.numPrb    = schCalcNumPrb(tbSize,sib1SchCfg->sib1Mcs, NUM_PDSCH_SYMBOL);
821    pdsch->pdschFreqAlloc.vrbPrbMapping       = 0; /* non-interleaved */
822    pdsch->pdschTimeAlloc.rowIndex            = 1;
823    /* This is Intel's requirement. PDSCH should start after PDSCH DRMS symbol */
824    pdsch->pdschTimeAlloc.timeAlloc.startSymb = 3; /* spec-38.214, Table 5.1.2.1-1 */
825    pdsch->pdschTimeAlloc.timeAlloc.numSymb   = NUM_PDSCH_SYMBOL;
826    pdsch->beamPdschInfo.numPrgs              = 1;
827    pdsch->beamPdschInfo.prgSize              = 1;
828    pdsch->beamPdschInfo.digBfInterfaces      = 0;
829    pdsch->beamPdschInfo.prg[0].pmIdx         = 0;
830    pdsch->beamPdschInfo.prg[0].beamIdx[0]    = 0;
831    pdsch->txPdschPower.powerControlOffset    = 0;
832    pdsch->txPdschPower.powerControlOffsetSS  = 0;
833
834 }
835
836 /**
837  * @brief cell config from MAC to SCH.
838  *
839  * @details
840  *
841  *     Function : macSchCellCfgReq
842  *      
843  *      This API is invoked by MAC to send cell config to SCH
844  *           
845  *  @param[in]  Pst            *pst
846  *  @param[in]  SchCellCfg     *schCellCfg
847  *  @return  int
848  *      -# ROK 
849  *      -# RFAILED 
850  **/
851 uint8_t SchHdlCellCfgReq(Pst *pst, SchCellCfg *schCellCfg)
852 {
853    uint8_t ret = ROK;
854    SchCellCb *cellCb;
855    SchCellCfgCfm schCellCfgCfm;
856    Pst rspPst;
857    Inst inst = pst->dstInst-1; 
858    uint8_t coreset0Idx = 0;
859    uint8_t numRbs = 0;
860    uint8_t offset = 0;
861    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
862    SchPdschConfig pdschCfg;
863
864 #ifdef CALL_FLOW_DEBUG_LOG
865    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_SCH_CELL_CFG\n");
866 #endif 
867
868    schInitCellCb(inst, schCellCfg);
869    cellCb = schCb[inst].cells[inst]; //cells is of MAX_CELLS, why inst
870    cellCb->macInst = pst->srcInst;
871
872    /* derive the SIB1 config parameters */
873    fillSchSib1Cfg(schCellCfg->numerology, schCellCfg->bandwidth, cellCb->numSlots,
874          &(schCellCfg->sib1SchCfg), schCellCfg->phyCellId,
875          schCellCfg->ssbSchCfg.ssbOffsetPointA);
876    memcpy(&cellCb->cellCfg, schCellCfg, sizeof(SchCellCfg));
877
878    /* Fill coreset frequencyDomainResource bitmap */
879    coreset0Idx = cellCb->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.coresetId;
880    numRbs = coresetIdxTable[coreset0Idx][1];
881    offset = coresetIdxTable[coreset0Idx][3];
882    fillCoresetFeqDomAllocMap(((cellCb->cellCfg.ssbSchCfg.ssbOffsetPointA - offset)/6), (numRbs/6), freqDomainResource);
883    covertFreqDomRsrcMapToIAPIFormat(freqDomainResource, \
884       cellCb->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc);
885
886    /* Fill K0 - K1 table for common cfg*/ 
887    BuildK0K1Table(cellCb, &cellCb->cellCfg.schInitialDlBwp.k0K1InfoTbl, true, cellCb->cellCfg.schInitialDlBwp.pdschCommon,
888    pdschCfg, DEFAULT_UL_ACK_LIST_COUNT, defaultUlAckTbl);
889    
890    BuildK2InfoTable(cellCb, cellCb->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList,\
891    cellCb->cellCfg.schInitialUlBwp.puschCommon.numTimeDomRsrcAlloc, &cellCb->cellCfg.schInitialUlBwp.msg3K2InfoTbl, \
892    &cellCb->cellCfg.schInitialUlBwp.k2InfoTbl);
893    /* Initializing global variables */
894    cellCb->actvUeBitMap = 0;
895    cellCb->boIndBitMap = 0;
896
897    /* Fill and send Cell config confirm */
898    memset(&rspPst, 0, sizeof(Pst));
899    FILL_PST_SCH_TO_MAC(rspPst, pst->dstInst);
900    rspPst.event = EVENT_SCH_CELL_CFG_CFM;
901
902    schCellCfgCfm.cellId = schCellCfg->cellId; 
903    schCellCfgCfm.rsp = RSP_OK;
904
905    ret = (*SchCellCfgCfmOpts[rspPst.selector])(&rspPst, &schCellCfgCfm);
906    return ret;
907
908 }
909
910 /*******************************************************************
911  *
912  * @brief Processes DL RLC BO info from MAC
913  *
914  * @details
915  *
916  *    Function : MacSchDlRlcBoInfo
917  *
918  *    Functionality:
919  *       Processes DL RLC BO info from MAC
920  *
921  * @params[in] 
922  * @return ROK     - success
923  *         RFAILED - failure
924  *
925  * ****************************************************************/
926 uint8_t MacSchDlRlcBoInfo(Pst *pst, DlRlcBoInfo *dlBoInfo)
927 {
928    uint8_t  lcId = 0;
929    uint16_t ueIdx = 0;
930    uint16_t slot = 0;
931 #ifdef NR_TDD
932    uint16_t slotIdx = 0;
933 #endif
934    SchUeCb *ueCb = NULLP;
935    SchCellCb *cell = NULLP;
936    SchDlSlotInfo *schDlSlotInfo = NULLP;
937    Inst  inst = pst->dstInst-SCH_INST_START;
938
939 #ifdef CALL_FLOW_DEBUG_LOG
940    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_DL_RLC_BO_INFO_TO_SCH\n");
941 #endif
942
943    DU_LOG("\nDEBUG  -->  SCH : Received RLC BO Status indication");
944    cell = schCb[inst].cells[inst];
945
946    if(cell == NULLP)
947    {
948       DU_LOG("\nERROR  -->  SCH : MacSchDlRlcBoInfo(): Cell does not exists");
949       return RFAILED;
950    }
951
952    GET_UE_IDX(dlBoInfo->crnti, ueIdx);
953    ueCb = &cell->ueCb[ueIdx-1];
954    lcId  = dlBoInfo->lcId;
955
956    if(lcId == SRB1_LCID || lcId == SRB2_LCID || lcId == SRB3_LCID || \
957          (lcId >= MIN_DRB_LCID && lcId <= MAX_DRB_LCID))
958    {
959       SET_ONE_BIT(ueIdx, cell->boIndBitMap);
960       if(ueCb->dlInfo.dlLcCtxt[lcId].lcId == lcId)
961       {
962          ueCb->dlInfo.dlLcCtxt[lcId].bo = dlBoInfo->dataVolume;
963       }
964       else
965       {
966          DU_LOG("ERROR --> SCH: LCID:%d is not configured in SCH Cb",lcId);
967          return RFAILED;
968       }
969    }
970    else if(lcId != SRB0_LCID)
971    {
972       DU_LOG("\nERROR  -->  SCH : Invalid LC Id %d in MacSchDlRlcBoInfo", lcId);
973       return RFAILED;
974    }
975
976    slot = (cell->slotInfo.slot + SCHED_DELTA + PHY_DELTA_DL + BO_DELTA) % cell->numSlots;
977 #ifdef NR_TDD
978    while(schGetSlotSymbFrmt(cell->slotFrmtBitMap, slot) != DL_SLOT)
979    {
980       slot = (slot + 1)%cell->numSlots;
981       slotIdx++;
982       if(slotIdx==cell->numSlots)
983       {
984          DU_LOG("\nERROR  -->  SCH : No DL Slot available");
985          return RFAILED;
986       }
987    }
988 #endif
989
990    schDlSlotInfo = cell->schDlSlotInfo[slot];
991
992    if(schDlSlotInfo == NULLP)
993    {
994       DU_LOG("\nERROR  -->  SCH : MacSchDlRlcBoInfo(): schDlSlotInfo does not exists");
995       return RFAILED;
996    }
997    SCH_ALLOC(schDlSlotInfo->dlMsgInfo, sizeof(DlMsgInfo));
998    if(schDlSlotInfo->dlMsgInfo == NULLP)
999    {
1000       DU_LOG("\nERROR  -->  SCH : Memory allocation failed for dlMsgInfo");
1001       schDlSlotInfo = NULL;
1002       return RFAILED;
1003    }
1004
1005    schDlSlotInfo->dlMsgInfo->crnti = dlBoInfo->crnti;
1006    schDlSlotInfo->dlMsgInfo->ndi = 1;
1007    schDlSlotInfo->dlMsgInfo->harqProcNum = 0;
1008    schDlSlotInfo->dlMsgInfo->dlAssignIdx = 0;
1009    schDlSlotInfo->dlMsgInfo->pucchTpc = 0;
1010    schDlSlotInfo->dlMsgInfo->pucchResInd = 0;
1011    schDlSlotInfo->dlMsgInfo->harqFeedbackInd = 0;
1012    schDlSlotInfo->dlMsgInfo->dciFormatId = 1;
1013    if(lcId == SRB0_LCID)
1014    {
1015       schDlSlotInfo->dlMsgInfo->isMsg4Pdu = true;
1016       schDlSlotInfo->dlMsgInfo->dlMsgPduLen = dlBoInfo->dataVolume;
1017    }
1018    return ROK;
1019 }
1020
1021 /*******************************************************************
1022  *
1023  * @brief Processes BSR indiation from MAC
1024  *
1025  * @details
1026  *
1027  *    Function : MacSchBsr
1028  *
1029  *    Functionality:
1030  *       Processes DL BSR from MAC
1031  *
1032  * @params[in]    Pst pst
1033  *                UlBufferStatusRptInd bsrInd
1034  * @return ROK     - success
1035  *         RFAILED - failure
1036  *
1037  * ****************************************************************/
1038 uint8_t MacSchBsr(Pst *pst, UlBufferStatusRptInd *bsrInd)
1039 {
1040    Inst           schInst       = pst->dstInst-SCH_INST_START;
1041    SchCellCb      *cellCb       = NULLP;
1042    SchUeCb        *ueCb         = NULLP;
1043    uint8_t        lcgIdx;
1044
1045 #ifdef CALL_FLOW_DEBUG_LOG
1046    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_SHORT_BSR\n");
1047 #endif
1048
1049    DU_LOG("\nDEBUG  -->  SCH : Received BSR");
1050    cellCb = schCb[schInst].cells[schInst];
1051    ueCb = schGetUeCb(cellCb, bsrInd->crnti);
1052
1053    /* store dataVolume per lcg in uecb */
1054    for(lcgIdx = 0; lcgIdx < bsrInd->numLcg; lcgIdx++)
1055    {
1056       ueCb->bsrInfo[lcgIdx].priority = 1; //TODO: determining LCG priority?
1057       ueCb->bsrInfo[lcgIdx].dataVol = bsrInd->dataVolInfo[lcgIdx].dataVol;
1058    }
1059    return ROK;
1060 }
1061
1062 /*******************************************************************
1063  *
1064  * @brief Processes SR UCI indication from MAC 
1065  *
1066  * @details
1067  *
1068  *    Function : MacSchSrUciInd
1069  *
1070  *    Functionality:
1071  *      Processes SR UCI indication from MAC
1072  *
1073  * @params[in] Post structure
1074  *             UCI Indication
1075  * @return ROK     - success
1076  *         RFAILED - failure
1077  *
1078  * ****************************************************************/
1079 uint8_t MacSchSrUciInd(Pst *pst, SrUciIndInfo *uciInd)
1080 {
1081    Inst  inst = pst->dstInst-SCH_INST_START;
1082
1083    SchUeCb   *ueCb; 
1084    SchCellCb *cellCb = schCb[inst].cells[inst];
1085
1086 #ifdef CALL_FLOW_DEBUG_LOG
1087    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_UCI_IND_TO_SCH\n");
1088 #endif
1089
1090    DU_LOG("\nDEBUG  -->  SCH : Received SR");
1091
1092    ueCb = schGetUeCb(cellCb, uciInd->crnti);
1093
1094    if(uciInd->numSrBits)
1095    {
1096       ueCb->srRcvd = true;
1097    }
1098    return ROK;
1099 }
1100
1101 /*******************************************************************
1102  *
1103  * @brief Allocates requested PRBs for DL
1104  *
1105  * @details
1106  *
1107  *    Function : allocatePrbDl
1108  *
1109  *    Functionality:
1110  *      Allocates requested PRBs in DL
1111  *      Keeps track of allocated PRB (using bitmap) and remaining PRBs
1112  *
1113  * @params[in] prbAlloc table
1114  *             Start symbol
1115  *             Number of symbols
1116  *             Start PRB
1117  *             Number of PRBs
1118  *
1119  * @return ROK     - success
1120  *         RFAILED - failure
1121  *
1122  * ****************************************************************/
1123 uint8_t allocatePrbDl(SchCellCb *cell, SlotTimingInfo slotTime, \
1124    uint8_t startSymbol, uint8_t symbolLength, uint16_t *startPrb, uint16_t numPrb)
1125 {
1126    uint8_t        symbol = 0;
1127    uint16_t       broadcastPrbStart=0, broadcastPrbEnd=0;
1128    FreePrbBlock   *freePrbBlock = NULLP;
1129    CmLList        *freePrbNode = NULLP;
1130    PduTxOccsaion  ssbOccasion=0, sib1Occasion=0;
1131    SchDlSlotInfo  *schDlSlotInfo = cell->schDlSlotInfo[slotTime.slot];
1132    SchPrbAlloc    *prbAlloc = &schDlSlotInfo->prbAlloc;
1133
1134    /* If startPrb is set to MAX_NUM_RB, it means startPrb is not known currently.
1135     * Search for an appropriate location in PRB grid and allocate requested resources */
1136    if(*startPrb == MAX_NUM_RB)
1137    {
1138       /* Check if SSB/SIB1 is also scheduled in this slot  */
1139       ssbOccasion = schCheckSsbOcc(cell, slotTime);
1140       sib1Occasion = schCheckSib1Occ(cell, slotTime);
1141
1142       if(ssbOccasion && sib1Occasion)
1143       {
1144          broadcastPrbStart = cell->cellCfg.ssbSchCfg.ssbOffsetPointA; 
1145          broadcastPrbEnd = broadcastPrbStart + SCH_SSB_NUM_PRB + cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.numPrb -1;
1146       }
1147       else if(ssbOccasion)
1148       {
1149          broadcastPrbStart = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
1150          broadcastPrbEnd = broadcastPrbStart + SCH_SSB_NUM_PRB -1;
1151       }
1152       else if(sib1Occasion)
1153       {
1154          broadcastPrbStart = cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.startPrb;
1155          broadcastPrbEnd = broadcastPrbStart + cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.numPrb -1;
1156       }
1157
1158       /* Iterate through all free PRB blocks */
1159       freePrbNode = prbAlloc->freePrbBlockList.first; 
1160       while(freePrbNode)
1161       {
1162          freePrbBlock = (FreePrbBlock *)freePrbNode->node; 
1163
1164          /* If broadcast message is scheduled in this slot, then check if its PRBs belong to the current free block.
1165           * Since SSB/SIB1 PRB location is fixed, these PRBs cannot be allocated to other message in same slot */
1166          if((ssbOccasion || sib1Occasion) && 
1167             ((broadcastPrbStart >= freePrbBlock->startPrb) && (broadcastPrbStart <= freePrbBlock->endPrb)) && \
1168             ((broadcastPrbEnd >= freePrbBlock->startPrb) && (broadcastPrbEnd <= freePrbBlock->endPrb)))
1169          {
1170             /* Implmentation is done such that highest-numbered free-RB is allocated first */ 
1171             if((freePrbBlock->endPrb > broadcastPrbEnd) && ((freePrbBlock->endPrb - broadcastPrbEnd) >= numPrb))
1172             {
1173                /* If sufficient free PRBs are available above bradcast message then,
1174                 * endPrb = freePrbBlock->endPrb
1175                 * startPrb = endPrb - numPrb +1;
1176                 */
1177                *startPrb = freePrbBlock->endPrb - numPrb +1;
1178                break;
1179             }
1180             else if((broadcastPrbStart > freePrbBlock->startPrb) && ((broadcastPrbStart - freePrbBlock->startPrb) >= numPrb))
1181             {
1182                /* If free PRBs are available below broadcast message then,
1183                 * endPrb = broadcastPrbStart - 1
1184                 * startPrb = endPrb - numPrb +1
1185                 */
1186                *startPrb = broadcastPrbStart - numPrb; 
1187                break;
1188             }
1189             else
1190             {
1191                freePrbNode = freePrbNode->next;
1192                continue;
1193             }
1194          }
1195          else
1196          {
1197             /* Check if requested number of blocks can be allocated from the current block */ 
1198             if (freePrbBlock->numFreePrb < numPrb)
1199             {
1200                freePrbNode = freePrbNode->next;
1201                continue;
1202             }
1203             *startPrb = freePrbBlock->endPrb - numPrb +1;
1204             break;  
1205          }
1206       }
1207
1208       /* If no free block can be used to allocated request number of RBs */
1209       if(*startPrb == MAX_NUM_RB)
1210          return RFAILED;
1211    }
1212
1213    /* If startPrb is known already, check if requested PRBs are available for allocation */
1214    else
1215    {
1216       freePrbNode = isPrbAvailable(&prbAlloc->freePrbBlockList, *startPrb, numPrb);
1217       if(!freePrbNode)
1218       {
1219          DU_LOG("\nERROR  -->  SCH: Requested DL PRB unavailable");
1220          return RFAILED;
1221       }
1222    }
1223
1224    /* Update bitmap to allocate PRBs */
1225    for(symbol=startSymbol; symbol < (startSymbol+symbolLength); symbol++)
1226    {
1227       if(fillPrbBitmap(prbAlloc->prbBitMap[symbol], *startPrb, numPrb) != ROK)
1228       {
1229          DU_LOG("\nERROR  -->  SCH: fillPrbBitmap() failed for symbol [%d] in DL", symbol);
1230          return RFAILED;
1231       }
1232    }
1233
1234    /* Update the remaining number for free PRBs */
1235    removeAllocatedPrbFromFreePrbList(&prbAlloc->freePrbBlockList, freePrbNode, *startPrb, numPrb);
1236
1237    return ROK;
1238 }
1239
1240 /*******************************************************************
1241  *
1242  * @brief Allocates requested PRBs for UL
1243  *
1244  * @details
1245  *
1246  *    Function : allocatePrbUl
1247  *
1248  *    Functionality:
1249  *      Allocates requested PRBs in UL
1250  *      Keeps track of allocated PRB (using bitmap) and remaining PRBs
1251  *
1252  * @params[in] prbAlloc table
1253  *             Start symbol
1254  *             Number of symbols
1255  *             Start PRB
1256  *             Number of PRBs
1257  *
1258  * @return ROK     - success
1259  *         RFAILED - failure
1260  *
1261  * ****************************************************************/
1262 uint8_t allocatePrbUl(SchCellCb *cell, SlotTimingInfo slotTime, \
1263    uint8_t startSymbol, uint8_t symbolLength, uint16_t *startPrb, uint16_t numPrb)
1264 {
1265    uint8_t        symbol = 0;
1266    uint16_t       prachStartPrb, prachNumPrb, prachEndPrb;
1267    bool           isPrachOccasion;
1268    FreePrbBlock   *freePrbBlock = NULLP;
1269    CmLList        *freePrbNode = NULLP;
1270    SchPrbAlloc    *prbAlloc = &cell->schUlSlotInfo[slotTime.slot]->prbAlloc;
1271
1272    /* If startPrb is set to MAX_NUM_RB, it means startPrb is not known currently.
1273     * Search for an appropriate location in PRB grid and allocate requested resources */
1274    if(*startPrb == MAX_NUM_RB)
1275    {
1276       /* Check if PRACH is also scheduled in this slot */
1277       isPrachOccasion = schCheckPrachOcc(cell, slotTime);
1278       if(isPrachOccasion)
1279       {
1280          prachStartPrb =  cell->cellCfg.schRachCfg.msg1FreqStart;
1281          prachNumPrb = schCalcPrachNumRb(cell);
1282          prachEndPrb = prachStartPrb + prachNumPrb -1;
1283       }
1284
1285       /* Iterate through all free PRB blocks */
1286       freePrbNode = prbAlloc->freePrbBlockList.first; 
1287       while(freePrbNode)
1288       {
1289          freePrbBlock = (FreePrbBlock *)freePrbNode->node; 
1290
1291          /* If PRACH is scheduled in this slot, then check if its PRBs belong to the current free block.
1292           * PRBs required for PRACH cannot be allocated to any other message */
1293          if((isPrachOccasion) &&
1294             ((prachStartPrb >= freePrbBlock->startPrb) && (prachStartPrb <= freePrbBlock->endPrb)) &&
1295             ((prachEndPrb >= freePrbBlock->startPrb) && (prachEndPrb <= freePrbBlock->endPrb)))
1296          {
1297             /* Implmentation is done such that highest-numbered free-RB is allocated first */ 
1298             if((freePrbBlock->endPrb > prachEndPrb) && ((freePrbBlock->endPrb - prachEndPrb) >= numPrb))
1299             {
1300                /* If sufficient free PRBs are available above PRACH message then,
1301                 * endPrb = freePrbBlock->endPrb
1302                 * startPrb = endPrb - numPrb +1;
1303                 */
1304                *startPrb = freePrbBlock->endPrb - numPrb +1;
1305                break;
1306             }
1307             else if((prachStartPrb > freePrbBlock->startPrb) && ((prachStartPrb - freePrbBlock->startPrb) >= numPrb))
1308             {
1309                /* If free PRBs are available below PRACH message then,
1310                 * endPrb = prachStartPrb - 1
1311                 * startPrb = endPrb - numPrb +1
1312                 */
1313                *startPrb = prachStartPrb - numPrb; 
1314                break;
1315             }
1316             else
1317             {
1318                freePrbNode = freePrbNode->next;
1319                continue;
1320             } 
1321          }
1322          else
1323          {
1324             /* Check if requested number of PRBs can be allocated from currect block */
1325             if(freePrbBlock->numFreePrb < numPrb)
1326             {
1327                freePrbNode = freePrbNode->next;
1328                continue;
1329             }
1330             *startPrb = freePrbBlock->endPrb - numPrb +1;
1331             break;
1332          }
1333       }
1334
1335       /* If no free block can be used to allocated requested number of RBs */
1336       if(*startPrb == MAX_NUM_RB)
1337          return RFAILED;
1338    }
1339    else
1340    {
1341       /* If startPrb is known already, check if requested PRBs are available for allocation */
1342       freePrbNode = isPrbAvailable(&prbAlloc->freePrbBlockList, *startPrb, numPrb);
1343       if(!freePrbNode)
1344       {
1345          DU_LOG("\nERROR  -->  SCH: Requested UL PRB unavailable");
1346          return RFAILED;
1347       }
1348    }
1349
1350    /* Update bitmap to allocate PRBs */
1351    for(symbol=startSymbol; symbol < (startSymbol+symbolLength); symbol++)
1352    {
1353       if(fillPrbBitmap(prbAlloc->prbBitMap[symbol], *startPrb, numPrb) != ROK)
1354       {
1355          DU_LOG("\nERROR  -->  SCH: fillPrbBitmap() failed for symbol [%d] in UL", symbol);
1356          return RFAILED;
1357       }
1358    }
1359
1360    /* Update the remaining number for free PRBs */
1361    removeAllocatedPrbFromFreePrbList(&prbAlloc->freePrbBlockList, freePrbNode, *startPrb, numPrb);
1362
1363    return ROK;
1364 }
1365  
1366 /*******************************************************************************
1367  *
1368  * @brief Try to find Best Free Block with Max Num PRB 
1369  *
1370  * @details
1371  *
1372  *    Function : searchLargestFreeBlockDL
1373  *
1374  *    Functionality:
1375  *     Finds the FreeBlock with MaxNum of FREE PRB considering SSB/SIB1 ocassions.
1376  *
1377  * @params[in] I/P > prbAlloc table (FreeBlock list)
1378  *             I/P > Slot timing Info
1379  *             O/P > Start PRB
1380  *       
1381  *
1382  * @return Max Number of Free PRB 
1383  *         If 0, then no Suitable Free Block
1384  *
1385  * ********************************************************************************/
1386
1387 uint16_t searchLargestFreeBlockDL(SchCellCb *cell, SlotTimingInfo slotTime,uint16_t *startPrb)
1388 {
1389    uint16_t       broadcastPrbStart=0, broadcastPrbEnd=0, maxFreePRB = 0;
1390    PduTxOccsaion  ssbOccasion=0, sib1Occasion=0;
1391    FreePrbBlock   *freePrbBlock = NULLP;
1392    CmLList        *freePrbNode = NULLP;
1393
1394    SchDlSlotInfo  *schDlSlotInfo = cell->schDlSlotInfo[slotTime.slot];
1395    SchPrbAlloc    *prbAlloc = &schDlSlotInfo->prbAlloc;
1396
1397    ssbOccasion = schCheckSsbOcc(cell, slotTime);
1398    sib1Occasion = schCheckSib1Occ(cell, slotTime);
1399
1400    if(ssbOccasion && sib1Occasion)
1401    {
1402       broadcastPrbStart = cell->cellCfg.ssbSchCfg.ssbOffsetPointA; 
1403       broadcastPrbEnd = broadcastPrbStart + SCH_SSB_NUM_PRB + cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.numPrb -1;
1404    }
1405    else if(ssbOccasion)
1406    {
1407       broadcastPrbStart = cell->cellCfg.ssbSchCfg.ssbOffsetPointA;
1408       broadcastPrbEnd = broadcastPrbStart + SCH_SSB_NUM_PRB -1;
1409    }
1410    else if(sib1Occasion)
1411    {
1412       broadcastPrbStart = cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.startPrb;
1413       broadcastPrbEnd = broadcastPrbStart + cell->cellCfg.sib1SchCfg.sib1PdschCfg.pdschFreqAlloc.freqAlloc.numPrb -1;
1414    }
1415
1416
1417    freePrbNode = prbAlloc->freePrbBlockList.first; 
1418    *startPrb = 0; /*Initialize the StartPRB to zero*/
1419    while(freePrbNode)
1420    {
1421       freePrbBlock = (FreePrbBlock *)freePrbNode->node;
1422
1423       /*For block with same numFreeBlocks, choose the one with HighestPRB range
1424        *Since FreeBLockList are arranged in Descending order of PRB range thus Skipping this block*/
1425       if(maxFreePRB >= freePrbBlock->numFreePrb) 
1426       {
1427          //skip this block
1428          freePrbNode = freePrbNode->next;
1429          continue;
1430       }
1431
1432       /* If broadcast message is scheduled in this slot, then check if its PRBs belong to the current free block.
1433        * Since SSB/SIB1 PRB location is fixed, these PRBs cannot be allocated to other message in same slot */
1434       if((ssbOccasion || sib1Occasion) && 
1435             ((broadcastPrbStart >= freePrbBlock->startPrb) && (broadcastPrbStart <= freePrbBlock->endPrb)) && \
1436             ((broadcastPrbEnd >= freePrbBlock->startPrb) && (broadcastPrbEnd <= freePrbBlock->endPrb)))
1437       {
1438
1439          /* Implmentation is done such that highest-numbered free-RB is Checked first
1440             and freePRB in this block is greater than Max till now */
1441          if((freePrbBlock->endPrb > broadcastPrbEnd) && ((freePrbBlock->endPrb - broadcastPrbEnd) > maxFreePRB))
1442          {
1443             /* If sufficient free PRBs are available above broadcast message*/
1444             *startPrb = broadcastPrbEnd + 1;
1445             maxFreePRB = (freePrbBlock->endPrb - broadcastPrbEnd);               
1446          }
1447          /*Also check the other freeBlock (i.e. Above the broadcast message) for MAX FREE PRB*/
1448          if((broadcastPrbStart > freePrbBlock->startPrb) && ((broadcastPrbStart - freePrbBlock->startPrb) > maxFreePRB))
1449          {
1450             /* If free PRBs are available below broadcast message*/
1451             *startPrb = freePrbBlock->startPrb;
1452             maxFreePRB = (broadcastPrbStart - freePrbBlock->startPrb);
1453          }
1454       }
1455       else  //Best Block
1456       {
1457          if(maxFreePRB < freePrbBlock->numFreePrb)
1458          {
1459             *startPrb = freePrbBlock->startPrb;
1460             maxFreePRB = freePrbBlock->numFreePrb;
1461          }
1462
1463       }
1464       freePrbNode = freePrbNode->next;
1465    }  
1466    return(maxFreePRB);
1467 }
1468
1469 /**********************************************************************
1470   End of file
1471  **********************************************************************/