Changes to make number of slots and BW generic
[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 SchCb schCb[SCH_MAX_INST];
52 void SchFillCfmPst(Pst *reqPst,Pst *cfmPst,RgMngmt *cfm);
53
54 /* local defines */
55 SchCellCfgCfmFunc SchCellCfgCfmOpts[] = 
56 {
57    packSchCellCfgCfm,     /* LC */
58    MacProcSchCellCfgCfm,  /* TC */
59    packSchCellCfgCfm      /* LWLC */
60 };
61
62
63 /**
64  * @brief Task Initiation function. 
65  *
66  * @details
67  *
68  *     Function : schActvInit
69  *     
70  *     This function is supplied as one of parameters during MAC's 
71  *     task registration. MAC will invoke this function once, after
72  *     it creates and attaches this TAPA Task to a system task.
73  *     
74  *  @param[in]  Ent Entity, the entity ID of this task.     
75  *  @param[in]  Inst Inst, the instance ID of this task.
76  *  @param[in]  Region Region, the region ID registered for memory 
77  *              usage of this task.
78  *  @param[in]  Reason Reason.
79  *  @return  int
80  *      -# ROK
81  **/
82 uint8_t schActvInit(Ent entity, Inst instId, Region region, Reason reason)
83 {
84    Inst inst = (instId  - SCH_INST_START);
85
86    /* Initialize the MAC TskInit structure to zero */
87    memset ((uint8_t *)&schCb[inst], 0, sizeof(schCb));
88
89    /* Initialize the MAC TskInit with received values */
90    schCb[inst].schInit.ent = entity;
91    schCb[inst].schInit.inst = inst;
92    schCb[inst].schInit.region = region;
93    schCb[inst].schInit.pool = 0;
94    schCb[inst].schInit.reason = reason;
95    schCb[inst].schInit.cfgDone = FALSE;
96    schCb[inst].schInit.acnt = FALSE;
97    schCb[inst].schInit.usta = FALSE;
98    schCb[inst].schInit.trc = FALSE;
99    schCb[inst].schInit.procId = ODU_GET_PROCID();
100
101    return ROK;
102 } /* schActvInit */
103
104 /**
105  * @brief Scheduler instance Configuration Handler. 
106  *
107  * @details
108  *
109  *     Function : SchInstCfg
110  *     
111  *     This function in called by SchProcGenCfgReq(). It handles the
112  *     general configurations of the scheduler instance. Returns
113  *     reason for success/failure of this function.
114  *     
115  *  @param[in]  RgCfg *cfg, the Configuaration information 
116  *  @return  uint16_t
117  *      -# LCM_REASON_NOT_APPL 
118  *      -# LCM_REASON_INVALID_MSGTYPE
119  *      -# LCM_REASON_MEM_NOAVAIL
120  **/
121 uint8_t SchInstCfg(RgCfg *cfg, Inst  dInst)
122 {
123    uint16_t ret = LCM_REASON_NOT_APPL;
124    Inst     inst = (dInst - SCH_INST_START);
125
126    printf("\nEntered SchInstCfg()");
127    /* Check if Instance Configuration is done already */
128    if (schCb[inst].schInit.cfgDone == TRUE)
129    {
130       return LCM_REASON_INVALID_MSGTYPE;
131    }
132    /* Update the Pst structure for LM interface */
133    memcpy(&schCb[inst].schInit.lmPst,
134          &cfg->s.schInstCfg.genCfg.lmPst,
135          sizeof(Pst));
136
137    schCb[inst].schInit.inst = inst;
138    schCb[inst].schInit.lmPst.srcProcId = schCb[inst].schInit.procId;
139    schCb[inst].schInit.lmPst.srcEnt = schCb[inst].schInit.ent;
140    schCb[inst].schInit.lmPst.srcInst = schCb[inst].schInit.inst +
141       SCH_INST_START;
142    schCb[inst].schInit.lmPst.event = EVTNONE;
143
144    schCb[inst].schInit.region = cfg->s.schInstCfg.genCfg.mem.region;
145    schCb[inst].schInit.pool = cfg->s.schInstCfg.genCfg.mem.pool;
146    schCb[inst].genCfg.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
147 #ifdef LTE_ADV
148    schCb[inst].genCfg.forceCntrlSrbBoOnPCel =  cfg->s.schInstCfg.genCfg.forceCntrlSrbBoOnPCel;
149    schCb[inst].genCfg.isSCellActDeactAlgoEnable =  cfg->s.schInstCfg.genCfg.isSCellActDeactAlgoEnable;
150 #endif
151    schCb[inst].genCfg.startCellId    = cfg->s.schInstCfg.genCfg.startCellId;
152 #if 0
153    /* Initialzie the timer queue */   
154    memset(&schCb[inst].tmrTq, 0, sizeof(CmTqType)*RGSCH_TQ_SIZE);
155    /* Initialize the timer control point */
156    memset(&schCb[inst].tmrTqCp, 0, sizeof(CmTqCp));
157    schCb[inst].tmrTqCp.tmrLen = RGSCH_TQ_SIZE;
158
159    /* SS_MT_TMR needs to be enabled as schActvTmr needs instance information */
160    /* Timer Registration request to SSI */
161    if (ODU_REG_TMR_MT(schCb[inst].schInit.ent, dInst,
162             (int)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
163    {
164       RLOG_ARG0(L_ERROR,DBG_INSTID,inst, "SchInstCfg(): Failed to "
165             "register timer.");
166       return (LCM_REASON_MEM_NOAVAIL);
167    }   
168 #endif               
169    /* Set Config done in TskInit */
170    schCb[inst].schInit.cfgDone = TRUE;
171    printf("\nScheduler gen config done");
172
173    return ret;
174 }
175
176 /**
177  * @brief Layer Manager Configuration request handler. 
178  *
179  * @details
180  *
181  *     Function : SchProcGenCfgReq
182  *     
183  *     This function handles the configuration
184  *     request received at scheduler instance from the Layer Manager.
185  *     -# Based on the cfg->hdr.elmId.elmnt value it invokes one of the
186  *        functions rgHdlGenCfg() or rgHdlSapCfg().
187  *     -# Invokes RgMiLrgSchCfgCfm() to send back the confirmation to the LM.
188  *     
189  *  @param[in]  Pst *pst, the post structure     
190  *  @param[in]  RgMngmt *cfg, the configuration parameter's structure
191  *  @return  S16
192  *      -# ROK
193  **/
194 uint8_t SchProcGenCfgReq(Pst *pst, RgMngmt *cfg)
195 {
196    uint8_t   ret = LCM_PRIM_OK;
197    uint16_t  reason = LCM_REASON_NOT_APPL;
198    RgMngmt   cfm;
199    Pst       cfmPst;
200
201    if(pst->dstInst < SCH_INST_START)
202    {
203       DU_LOG("\nERROR  -->  SCH : Invalid inst ID");
204       DU_LOG("\nERROR  -->  SCH : SchProcGenCfgReq(): "
205             "pst->dstInst=%d SCH_INST_START=%d", pst->dstInst,SCH_INST_START); 
206       return ROK;
207    }
208    printf("\nSCH : Received scheduler gen config");
209    /* Fill the post structure for sending the confirmation */
210    memset(&cfmPst, 0 , sizeof(Pst));
211    SchFillCfmPst(pst, &cfmPst, cfg);
212
213    memset(&cfm, 0, sizeof(RgMngmt));
214
215 #ifdef LMINT3
216    cfm.hdr.transId =
217       cfg->hdr.transId;
218 #endif
219
220    cfm.hdr.elmId.elmnt = cfg->hdr.elmId.elmnt;
221    switch(cfg->hdr.elmId.elmnt)
222    {
223       case STSCHINST:
224          reason = SchInstCfg(&cfg->t.cfg,pst->dstInst );
225          break;
226       default:
227          ret = LCM_PRIM_NOK;
228          reason = LCM_REASON_INVALID_ELMNT;
229          DU_LOG("\nERROR  -->  SCH : Invalid Elmnt=%d", cfg->hdr.elmId.elmnt);
230          break;
231    }
232
233    if (reason != LCM_REASON_NOT_APPL)
234    {
235       ret = LCM_PRIM_NOK;
236    }
237
238    cfm.cfm.status = ret;
239    cfm.cfm.reason = reason;
240
241    SchSendCfgCfm(&cfmPst, &cfm);
242    /*   SPutSBuf(pst->region, pst->pool, (Data *)cfg, sizeof(RgMngmt)); */
243
244    return ROK;
245 }/*-- SchProcGenCfgReq --*/
246
247 /**
248  * @brief slot indication from MAC to SCH.
249  *
250  * @details
251  *
252  *     Function : MacSchSlotInd 
253  *      
254  *      This API is invoked by PHY to indicate slot indication to Scheduler for
255  *      a cell.
256  *           
257  *  @param[in]  Pst            *pst
258  *  @param[in]  SlotIndInfo    *slotInd
259  *  @return  S16
260  *      -# ROK 
261  *      -# RFAILED 
262  **/
263 uint8_t MacSchSlotInd(Pst *pst, SlotIndInfo *slotInd)
264 {
265    Inst  inst = pst->dstInst-SCH_INST_START;
266
267    schProcessSlotInd(slotInd, inst);
268
269    return ROK;
270 }  /* MacSchSlotInd */
271
272 /*******************************************************************
273  *
274  * @brief Processes Rach indication from MAC 
275  *
276  * @details
277  *
278  *    Function : MacSchRachInd
279  *
280  *    Functionality:
281  *      Processes Rach indication from MAC
282  *
283  * @params[in] 
284  * @return ROK     - success
285  *         RFAILED - failure
286  *
287  * ****************************************************************/
288 uint8_t MacSchRachInd(Pst *pst, RachIndInfo *rachInd)
289 {
290    Inst  inst = pst->dstInst-SCH_INST_START;
291    DU_LOG("\nINFO  -->  SCH : Received Rach indication");
292    schProcessRachInd(rachInd, inst);
293    return ROK;
294 }
295
296 /*******************************************************************
297  *
298  * @brief Processes CRC indication from MAC 
299  *
300  * @details
301  *
302  *    Function : MacSchCrcInd
303  *
304  *    Functionality:
305  *      Processes CRC indication from MAC
306  *
307  * @params[in] Post structure
308  *             Crc Indication
309  * @return ROK     - success
310  *         RFAILED - failure
311  *
312  * ****************************************************************/
313 uint8_t MacSchCrcInd(Pst *pst, CrcIndInfo *crcInd)
314 {
315    switch(crcInd->crcInd[0])
316    {
317       case CRC_FAILED:
318          DU_LOG("\nDEBUG  -->  SCH : Received CRC indication. CRC Status [FAILURE]");
319          break;
320       case CRC_PASSED:
321          DU_LOG("\nDEBUG  -->  SCH : Received CRC indication. CRC Status [PASS]");
322          break;
323       default:
324          DU_LOG("\nDEBUG  -->  SCH : Invalid CRC state %d", crcInd->crcInd[0]);
325          return RFAILED;
326    }
327    return ROK;
328 }
329
330
331 /**
332  * @brief inti cellCb based on cellCfg
333  *
334  * @details
335  *
336  *     Function : schInitCellCb 
337  *      
338  *      This API is invoked after receiving schCellCfg
339  *           
340  *  @param[in]  schCellCb *cell
341  *  @param[in]  SchCellCfg *schCellCfg
342  *  @return  int
343  *      -# ROK 
344  *      -# RFAILED 
345  **/
346 uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
347 {
348    SchCellCb *cell;
349    SCH_ALLOC(cell, sizeof(SchCellCb));
350    if(!cell)
351    {
352       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
353       return RFAILED;
354    }
355
356    cell->cellId = schCellCfg->cellId; 
357    cell->instIdx = inst;
358    switch(schCellCfg->numerology)
359    {
360       case SCH_NUMEROLOGY_0:
361          {
362             cell->numSlots = SCH_MU0_NUM_SLOTS;
363          }
364          break;
365       case SCH_NUMEROLOGY_1:
366          {
367             cell->numSlots = SCH_MU1_NUM_SLOTS;
368          }
369          break;
370       case SCH_NUMEROLOGY_2:
371          {
372             cell->numSlots = SCH_MU2_NUM_SLOTS;
373          }
374          break;
375       case SCH_NUMEROLOGY_3:
376          {
377             cell->numSlots = SCH_MU3_NUM_SLOTS;
378          }
379          break;
380       case SCH_NUMEROLOGY_4:
381          {
382             cell->numSlots = SCH_MU4_NUM_SLOTS;
383          }
384          break;
385       default:
386          DU_LOG("\nERROR  -->  SCH : Numerology %d not supported", schCellCfg->numerology);
387    }
388
389    SCH_ALLOC(cell->schDlSlotInfo, cell->numSlots * sizeof(SchDlSlotInfo*));
390    if(!cell->schDlSlotInfo)
391    {
392       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schDlSlotInfo");
393       return RFAILED;
394    }
395
396    SCH_ALLOC(cell->schUlSlotInfo, cell->numSlots * sizeof(SchUlSlotInfo*));
397    if(!cell->schUlSlotInfo)
398    {
399       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schUlSlotInfo");
400       return RFAILED;
401    }
402
403    for(uint8_t idx=0; idx<cell->numSlots; idx++)
404    {
405       SchDlSlotInfo *schDlSlotInfo;
406       SchUlSlotInfo *schUlSlotInfo;
407
408       /* DL Alloc */
409       SCH_ALLOC(schDlSlotInfo, sizeof(SchDlSlotInfo));
410       if(!schDlSlotInfo)
411       {
412          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
413          return RFAILED;
414       }
415
416       /* UL Alloc */
417       SCH_ALLOC(schUlSlotInfo, sizeof(SchUlSlotInfo));
418       if(!schUlSlotInfo)
419       {
420          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
421          return RFAILED;
422       }
423
424       schInitDlSlot(schDlSlotInfo);
425       schInitUlSlot(schUlSlotInfo);
426
427       cell->schDlSlotInfo[idx] = schDlSlotInfo;
428       cell->schUlSlotInfo[idx] = schUlSlotInfo;
429
430    }
431    schCb[inst].cells[inst] = cell;
432
433    DU_LOG("\nINFO  -->  SCH : Cell init completed for cellId:%d", cell->cellId);
434
435    return ROK;   
436 }
437
438 /**
439  * @brief Fill SIB1 configuration
440  *
441  * @details
442  *
443  *     Function : fillSchSib1Cfg
444  *
445  *     Fill SIB1 configuration
446  *
447  *  @param[in]  uint8_t bandwidth : total available bandwidth
448  *              uint8_t numSlots : total slots per SFN
449  *              SchSib1Cfg *sib1SchCfg : cfg to be filled
450  *              uint16_t pci : physical cell Id
451  *              uint8_t offsetPointA : offset
452  *  @return  void
453  **/
454 void fillSchSib1Cfg(uint8_t bandwidth, uint8_t numSlots, SchSib1Cfg *sib1SchCfg, uint16_t pci, uint8_t offsetPointA)
455 {
456    uint8_t coreset0Idx = 0;
457    uint8_t searchSpace0Idx = 0;
458    //uint8_t ssbMuxPattern = 0;
459    uint8_t numRbs = 0;
460    uint8_t numSymbols = 0;
461    uint8_t offset = 0;
462    uint8_t oValue = 0;
463    //uint8_t numSearchSpacePerSlot = 0;
464    uint8_t mValue = 0;
465    uint8_t firstSymbol = 0; /* need to calculate using formula mentioned in 38.213 */
466    uint8_t slotIndex = 0;
467    uint8_t FreqDomainResource[6] = {0};
468    uint16_t tbSize = 0;
469    uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
470
471    PdcchCfg *pdcch = &(sib1SchCfg->sib1PdcchCfg);
472    PdschCfg *pdsch = &(sib1SchCfg->sib1PdschCfg);
473    BwpCfg *bwp = &(sib1SchCfg->bwp);
474
475    coreset0Idx     = sib1SchCfg->coresetZeroIndex;
476    searchSpace0Idx = sib1SchCfg->searchSpaceZeroIndex;
477
478    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
479    //ssbMuxPattern = coresetIdxTable[coreset0Idx][0];
480    numRbs        = coresetIdxTable[coreset0Idx][1];
481    numSymbols    = coresetIdxTable[coreset0Idx][2];
482    offset        = coresetIdxTable[coreset0Idx][3];
483
484    /* derive the search space params from table 13-11 spec 38.213 */
485    oValue                = searchSpaceIdxTable[searchSpace0Idx][0];
486    //numSearchSpacePerSlot = searchSpaceIdxTable[searchSpace0Idx][1];
487    mValue                = searchSpaceIdxTable[searchSpace0Idx][2];
488    firstSymbol           = searchSpaceIdxTable[searchSpace0Idx][3];
489
490    /* calculate the n0, need to add the formulae, as of now the value is 0 
491     * Need to add the even and odd values of i during configuration 
492     * [(O . 2^u + i . M )  ] mod numSlotsPerSubframe 
493     * assuming u = 0, i = 0, numSlotsPerSubframe = 10
494     * Also, from this configuration, coreset0 is only on even subframe */
495    slotIndex = ((oValue * 1) + (0 * mValue)) % numSlots; 
496    sib1SchCfg->n0 = slotIndex;
497
498    /* calculate the PRBs */
499    freqDomRscAllocType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
500
501    /* fill BWP */
502    switch(bandwidth)
503    {
504       case BANDWIDTH_20MHZ:
505          {
506             bwp->freqAlloc.numPrb = TOTAL_PRB_20MHZ_MU0;
507          }
508          break;
509       case BANDWIDTH_100MHZ:
510          {
511             bwp->freqAlloc.numPrb = TOTAL_PRB_100MHZ_MU1;
512          }
513          break;
514       default:
515          DU_LOG("\nERROR  -->  SCH : Bandwidth %d not supported", bandwidth);
516
517    }
518    bwp->freqAlloc.startPrb = 0;
519    bwp->subcarrierSpacing  = 0;         /* 15Khz */
520    bwp->cyclicPrefix       = 0;              /* normal */
521
522    /* fill the PDCCH PDU */
523    pdcch->coresetCfg.coreSetSize = numRbs;
524    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
525    pdcch->coresetCfg.durationSymbols = numSymbols;
526    memcpy(pdcch->coresetCfg.freqDomainResource,FreqDomainResource,6);
527    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
528    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
529    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
530    pdcch->coresetCfg.coreSetType = 0;
531    pdcch->coresetCfg.shiftIndex = pci;
532    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
533    pdcch->numDlDci = 1;
534    pdcch->dci.rnti = SI_RNTI;
535    pdcch->dci.scramblingId = pci;
536    pdcch->dci.scramblingRnti = 0;
537    pdcch->dci.cceIndex = 0;
538    pdcch->dci.aggregLevel = 4;
539    pdcch->dci.beamPdcchInfo.numPrgs = 1;
540    pdcch->dci.beamPdcchInfo.prgSize = 1;
541    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
542    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
543    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
544    pdcch->dci.txPdcchPower.powerValue = 0;
545    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
546    /* Storing pdschCfg pointer here. Required to access pdsch config while
547       fillig up pdcch pdu */
548    pdcch->dci.pdschCfg = pdsch; 
549
550    /* fill the PDSCH PDU */
551    uint8_t cwCount = 0;
552    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
553    pdsch->rnti = 0xFFFF; /* SI-RNTI */
554    pdsch->pduIndex = 0;
555    pdsch->numCodewords = 1;
556    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
557    {
558       pdsch->codeword[cwCount].targetCodeRate = 308;
559       pdsch->codeword[cwCount].qamModOrder = 2;
560       pdsch->codeword[cwCount].mcsIndex = sib1SchCfg->sib1Mcs;
561       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
562       pdsch->codeword[cwCount].rvIndex = 0;
563       tbSize = schCalcTbSize(sib1SchCfg->sib1PduLen);
564       pdsch->codeword[cwCount].tbSize = tbSize;
565    }
566    pdsch->dataScramblingId                   = pci;
567    pdsch->numLayers                          = 1;
568    pdsch->transmissionScheme                 = 0;
569    pdsch->refPoint                           = 0;
570    pdsch->dmrs.dlDmrsSymbPos                 = 2;
571    pdsch->dmrs.dmrsConfigType                = 0; /* type-1 */
572    pdsch->dmrs.dlDmrsScramblingId            = pci;
573    pdsch->dmrs.scid                          = 0;
574    pdsch->dmrs.numDmrsCdmGrpsNoData          = 1;
575    pdsch->dmrs.dmrsPorts                     = 0;
576    pdsch->dmrs.mappingType                   = DMRS_MAP_TYPE_A; /* Type-A */
577    pdsch->dmrs.nrOfDmrsSymbols               = NUM_DMRS_SYMBOLS;
578    pdsch->dmrs.dmrsAddPos                    = DMRS_ADDITIONAL_POS;
579
580    pdsch->pdschFreqAlloc.resourceAllocType   = 1; /* RAT type-1 RIV format */
581    pdsch->pdschFreqAlloc.freqAlloc.startPrb  = offset + SCH_SSB_NUM_PRB; /* the RB numbering starts from coreset0,
582                                                                             and PDSCH is always above SSB */
583    pdsch->pdschFreqAlloc.freqAlloc.numPrb    = schCalcNumPrb(tbSize,sib1SchCfg->sib1Mcs,numPdschSymbols);
584    pdsch->pdschFreqAlloc.vrbPrbMapping       = 0; /* non-interleaved */
585    pdsch->pdschTimeAlloc.rowIndex            = 1;
586    pdsch->pdschTimeAlloc.timeAlloc.startSymb = 2; /* spec-38.214, Table 5.1.2.1-1 */
587    pdsch->pdschTimeAlloc.timeAlloc.numSymb   = numPdschSymbols;
588    pdsch->beamPdschInfo.numPrgs              = 1;
589    pdsch->beamPdschInfo.prgSize              = 1;
590    pdsch->beamPdschInfo.digBfInterfaces      = 0;
591    pdsch->beamPdschInfo.prg[0].pmIdx         = 0;
592    pdsch->beamPdschInfo.prg[0].beamIdx[0]    = 0;
593    pdsch->txPdschPower.powerControlOffset    = 0;
594    pdsch->txPdschPower.powerControlOffsetSS  = 0;
595
596 }
597
598 /**
599  * @brief Fill SSB start symbol
600  *
601  * @details
602  *
603  *     Function : fillSsbStartSymb 
604  *      
605  *      This API stores SSB start index per beam
606  *           
607  *  @param[in]  SchCellCb     *cellCb
608  *  @return  int
609  *      -# ROK 
610  *      -# RFAILED 
611  **/
612 void fillSsbStartSymb(SchCellCb *cellCb)
613 {
614    uint8_t cnt, scs;
615
616    scs = cellCb->cellCfg.ssbSchCfg.scsCommon;
617    uint8_t ssbStartSymbArr[SCH_MAX_SSB_BEAM];
618
619    memset(ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
620    /* Determine value of "n" based on Section 4.1 of 3GPP TS 38.213 */
621    switch(scs)
622    {
623       case SCH_SCS_15KHZ:
624          {
625             uint8_t symbIdx=0;
626             cnt = 2;/* n = 0, 1 for SCS = 15KHz */
627             for(uint8_t idx=0; idx<cnt; idx++)
628             {
629                /* start symbol determined using {2, 8} + 14n */
630                ssbStartSymbArr[symbIdx++]       = 2 + SCH_SYMBOL_PER_SLOT*idx;
631                ssbStartSymbArr[symbIdx++]       = 8 + SCH_SYMBOL_PER_SLOT*idx;
632             }
633          }
634          break;
635       default:
636          DU_LOG("\nERROR  -->  SCH : SCS %d is currently not supported", scs);
637    }
638    memset(cellCb->ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
639    memcpy(cellCb->ssbStartSymbArr, ssbStartSymbArr, SCH_MAX_SSB_BEAM);
640
641 }
642
643 /**
644  * @brief cell config from MAC to SCH.
645  *
646  * @details
647  *
648  *     Function : macSchCellCfgReq
649  *      
650  *      This API is invoked by MAC to send cell config to SCH
651  *           
652  *  @param[in]  Pst            *pst
653  *  @param[in]  SchCellCfg     *schCellCfg
654  *  @return  int
655  *      -# ROK 
656  *      -# RFAILED 
657  **/
658 uint8_t SchHdlCellCfgReq(Pst *pst, SchCellCfg *schCellCfg)
659 {
660    uint8_t ret = ROK;
661    SchCellCb *cellCb;
662    SchCellCfgCfm schCellCfgCfm;
663    Pst rspPst;
664    Inst inst = pst->dstInst-1; 
665
666    schInitCellCb(inst, schCellCfg);
667    cellCb = schCb[inst].cells[inst]; //cells is of MAX_CELLS, why inst
668    cellCb->macInst = pst->srcInst;
669
670    /* derive the SIB1 config parameters */
671    fillSchSib1Cfg(schCellCfg->bandwidth, cellCb->numSlots,
672          &(schCellCfg->sib1SchCfg), schCellCfg->phyCellId,
673          schCellCfg->ssbSchCfg.ssbOffsetPointA);
674    memcpy(&cellCb->cellCfg, schCellCfg, sizeof(SchCellCfg));
675
676    /* Initializing global variables */
677    cellCb->actvUeBitMap = 0;
678    cellCb->boIndBitMap = 0;
679
680    /* Fill and send Cell config confirm */
681    memset(&rspPst, 0, sizeof(Pst));
682    FILL_PST_SCH_TO_MAC(rspPst, pst->dstInst);
683    rspPst.event = EVENT_SCH_CELL_CFG_CFM;
684
685    schCellCfgCfm.cellId = schCellCfg->cellId; 
686    schCellCfgCfm.rsp = RSP_OK;
687
688    ret = (*SchCellCfgCfmOpts[rspPst.selector])(&rspPst, &schCellCfgCfm);
689
690    return ret;
691
692 }
693
694 /*******************************************************************
695  *
696  * @brief Processes DL RLC BO info from MAC
697  *
698  * @details
699  *
700  *    Function : MacSchDlRlcBoInfo
701  *
702  *    Functionality:
703  *       Processes DL RLC BO info from MAC
704  *
705  * @params[in] 
706  * @return ROK     - success
707  *         RFAILED - failure
708  *
709  * ****************************************************************/
710 uint8_t MacSchDlRlcBoInfo(Pst *pst, DlRlcBoInfo *dlBoInfo)
711 {
712    uint8_t  lcId = 0;
713    uint16_t ueIdx = 0;
714    uint16_t slot;
715    SchUeCb *ueCb = NULLP;
716    SchCellCb *cell = NULLP;
717    SchDlSlotInfo *schDlSlotInfo = NULLP;
718
719    Inst  inst = pst->dstInst-SCH_INST_START;
720    DU_LOG("\nDEBUG  -->  SCH : Received RLC BO Status indication");
721    cell = schCb[inst].cells[inst];
722
723    GET_UE_IDX(dlBoInfo->crnti, ueIdx);
724    ueCb = &cell->ueCb[ueIdx-1];
725    lcId  = dlBoInfo->lcId;
726
727    if(lcId == SRB1_LCID || lcId == SRB2_LCID || lcId == SRB3_LCID || \
728       (lcId >= MIN_DRB_LCID && lcId <= MAX_DRB_LCID))
729    {
730       SET_ONE_BIT(ueIdx, cell->boIndBitMap);
731       ueCb->dlInfo.dlLcCtxt[lcId].bo = dlBoInfo->dataVolume;
732    }
733    else if(lcId != SRB0_LCID)
734    {
735       DU_LOG("\nERROR  -->  SCH : Invalid LC Id %d in MacSchDlRlcBoInfo", lcId);
736       return RFAILED;
737    }
738
739    slot = (cell->slotInfo.slot + SCHED_DELTA + PHY_DELTA + BO_DELTA) % cell->numSlots;
740    schDlSlotInfo = cell->schDlSlotInfo[slot];
741
742    SCH_ALLOC(schDlSlotInfo->dlMsgInfo, sizeof(DlMsgInfo));
743    if(!schDlSlotInfo->dlMsgInfo)
744    {
745       DU_LOG("\nERROR  -->  SCH : Memory allocation failed for dlMsgInfo");
746       schDlSlotInfo = NULL;
747       return RFAILED;
748    }
749    schDlSlotInfo->dlMsgInfo->crnti = dlBoInfo->crnti;
750    schDlSlotInfo->dlMsgInfo->ndi = 1;
751    schDlSlotInfo->dlMsgInfo->harqProcNum = 0;
752    schDlSlotInfo->dlMsgInfo->dlAssignIdx = 0;
753    schDlSlotInfo->dlMsgInfo->pucchTpc = 0;
754    schDlSlotInfo->dlMsgInfo->pucchResInd = 0;
755    schDlSlotInfo->dlMsgInfo->harqFeedbackInd = 0;
756    schDlSlotInfo->dlMsgInfo->dciFormatId = 1;
757    if(lcId == SRB0_LCID)
758       schDlSlotInfo->dlMsgInfo->isMsg4Pdu = true;
759
760    return ROK;
761 }
762
763 /*******************************************************************
764  *
765  * @brief Processes BSR indiation from MAC
766  *
767  * @details
768  *
769  *    Function : MacSchBsr
770  *
771  *    Functionality:
772  *       Processes DL BSR from MAC
773  *
774  * @params[in]    Pst pst
775  *                UlBufferStatusRptInd bsrInd
776  * @return ROK     - success
777  *         RFAILED - failure
778  *
779  * ****************************************************************/
780 uint8_t MacSchBsr(Pst *pst, UlBufferStatusRptInd *bsrInd)
781 {
782    Inst           schInst       = pst->dstInst-SCH_INST_START;
783    SchCellCb      *cellCb       = NULLP;
784    SchUeCb        *ueCb         = NULLP;
785    uint8_t        lcgIdx;
786
787    DU_LOG("\nDEBUG  -->  SCH : Received BSR");
788    cellCb = schCb[schInst].cells[schInst];
789    ueCb = schGetUeCb(cellCb, bsrInd->crnti);
790
791    /* store dataVolume per lcg in uecb */
792    for(lcgIdx = 0; lcgIdx < bsrInd->numLcg; lcgIdx++)
793    {
794       ueCb->bsrInfo[lcgIdx].priority = 1; //TODO: determining LCG priority?
795       ueCb->bsrInfo[lcgIdx].dataVol = bsrInd->dataVolInfo[lcgIdx].dataVol;
796    }
797    return ROK;
798 }
799
800 /*******************************************************************
801  *
802  * @brief Processes SR UCI indication from MAC 
803  *
804  * @details
805  *
806  *    Function : MacSchSrUciInd
807  *
808  *    Functionality:
809  *      Processes SR UCI indication from MAC
810  *
811  * @params[in] Post structure
812  *             UCI Indication
813  * @return ROK     - success
814  *         RFAILED - failure
815  *
816  * ****************************************************************/
817 uint8_t MacSchSrUciInd(Pst *pst, SrUciIndInfo *uciInd)
818 {
819    Inst  inst = pst->dstInst-SCH_INST_START;
820
821    SchUeCb   *ueCb; 
822    SchCellCb *cellCb = schCb[inst].cells[inst];
823
824    DU_LOG("\nDEBUG  -->  SCH : Received SR");
825
826    ueCb = schGetUeCb(cellCb, uciInd->crnti);
827
828    if(uciInd->numSrBits)
829    {
830       ueCb->srRcvd = true;
831    }
832    return ROK;
833 }
834 /**********************************************************************
835   End of file
836  **********************************************************************/