Fixes for SSB transmission in Radio mode integration [Issue-ID: ODUHIGH-267]
[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    DU_LOG("\nDEBUG  -->  SCH : Entered 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
153    /* Initialzie the timer queue */   
154    memset(&schCb[inst].tmrTq, 0, sizeof(CmTqType) * SCH_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 system services */
161    if (ODU_REG_TMR_MT(schCb[inst].schInit.ent, dInst,
162             (int)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
163    {
164       DU_LOG("\nERROR  -->  SCH : SchInstCfg(): Failed to "
165             "register timer.");
166       return (LCM_REASON_MEM_NOAVAIL);
167    }   
168               
169    /* Set Config done in TskInit */
170    schCb[inst].schInit.cfgDone = TRUE;
171    DU_LOG("\nINFO  -->  SCH : Scheduler 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    DU_LOG("\nINFO -->  SCH : 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 #ifdef NR_TDD
331 /**
332  *@brief Returns TDD periodicity in micro seconds
333  *
334  * @details
335  * 
336  * Function : schGetPeriodicityInMsec 
337  * 
338  * This API retunrs TDD periodicity in micro seconds
339  * 
340  * @param[in] DlUlTxPeriodicity 
341  * @return  periodicityInMsec
342  * **/
343
344 uint16_t schGetPeriodicityInMsec(DlUlTxPeriodicity tddPeriod)
345 {
346    uint16_t  periodicityInMsec = 0;
347    switch(tddPeriod)
348    {
349       case TX_PRDCTY_MS_0P5:
350       {
351          periodicityInMsec = 500;
352          break;
353       }
354       case TX_PRDCTY_MS_0P625:
355       {
356          periodicityInMsec = 625;
357          break;
358       }
359       case TX_PRDCTY_MS_1:
360       {
361          periodicityInMsec = 1000;
362          break;
363       }
364       case TX_PRDCTY_MS_1P25:
365       {
366          periodicityInMsec = 1250;
367          break;
368       }
369       case TX_PRDCTY_MS_2:
370       {
371          periodicityInMsec = 2000;
372          break;
373       }
374       case TX_PRDCTY_MS_2P5:
375       {
376          periodicityInMsec = 2500;
377          break;
378       }
379       case TX_PRDCTY_MS_5:
380       {
381          periodicityInMsec = 5000;
382          break;
383       }
384       case TX_PRDCTY_MS_10:
385       {
386          periodicityInMsec = 10000;
387          break;
388       }
389       default:
390       {
391          DU_LOG("\nERROR  -->  SCH : Invalid DlUlTxPeriodicity:%d", tddPeriod);
392       }
393    }
394
395    return periodicityInMsec;
396 }
397
398
399 /**
400  * @brief init TDD slot config 
401  *
402  * @details
403  *
404  *     Function : schInitTddSlotCfg 
405  *      
406  *      This API is invoked after receiving schCellCfg
407  *           
408  *  @param[in]  schCellCb *cell
409  *  @param[in]  SchCellCfg *schCellCfg
410  *  @return  void
411  **/
412 void schInitTddSlotCfg(SchCellCb *cell, SchCellCfg *schCellCfg)
413 {
414    uint16_t periodicityInMicroSec = 0;
415    uint32_t slotBitPos, symbBitPos, bitMask;
416    int8_t slotIdx, symbIdx;
417
418    periodicityInMicroSec = schGetPeriodicityInMsec(schCellCfg->tddCfg.tddPeriod);
419    cell->numSlotsInPeriodicity = (periodicityInMicroSec * pow(2, schCellCfg->numerology))/1000;
420 cell->slotFrmtBitMap = 0;
421    cell->symbFrmtBitMap = 0;
422    slotBitPos = (cell->numSlotsInPeriodicity*2)-1; /* considering 2 bits to represent a slot */
423    symbBitPos = (MAX_SYMB_PER_SLOT*2)-1; /* considering 2 bits to represent a symbol */
424    for(slotIdx = cell->numSlotsInPeriodicity-1; slotIdx >= 0; slotIdx--)
425    {
426       symbIdx = 0;
427       /* If the first and last symbol are the same, the entire slot is the same type */
428       if((schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx] == schCellCfg->tddCfg.slotCfg[slotIdx][MAX_SYMB_PER_SLOT-1]) &&
429               schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx] != FLEXI_SLOT)
430       {
431          switch(schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx])
432          {
433             case DL_SLOT:
434             {
435                /*BitMap to be set to 00 */
436                bitMask = 1<<slotBitPos;
437                cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((0<<slotBitPos) & bitMask);
438                slotBitPos--;
439                bitMask = 1<<slotBitPos;
440                cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((0<<slotBitPos) & bitMask);
441                slotBitPos--;
442                break;
443             }
444             case UL_SLOT:
445             {
446                /*BitMap to be set to 01 */
447                bitMask = 1<<slotBitPos;
448                cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((0<<slotBitPos) & bitMask);
449                slotBitPos--;
450                bitMask = 1<<slotBitPos;
451                cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((1<<slotBitPos) & bitMask);
452                slotBitPos--;
453                break;
454             }
455             default:
456                DU_LOG("\nERROR  -->  SCH : Invalid slot Config in schInitTddSlotCfg");
457            }
458          continue;
459       }
460       /* slot config is flexible. First set slotBitMap to 10 */
461       bitMask = 1<<slotBitPos;
462       cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((1<<slotBitPos) & bitMask);
463       slotBitPos--;
464       bitMask = 1<<slotBitPos;
465       cell->slotFrmtBitMap = (cell->slotFrmtBitMap & ~(bitMask)) | ((0<<slotBitPos) & bitMask);
466       slotBitPos--;
467       /* Now set symbol bitmap */ 
468       for(symbIdx = MAX_SYMB_PER_SLOT-1; symbIdx >= 0; symbIdx--)
469       {
470          switch(schCellCfg->tddCfg.slotCfg[slotIdx][symbIdx])
471          {
472             case DL_SLOT:
473             {
474                /*symbol BitMap to be set to 00 */
475                bitMask = 1<<symbBitPos;
476                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((0<<symbBitPos) & bitMask);
477                symbBitPos--;
478                bitMask = 1<<symbBitPos;
479                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((0<<symbBitPos) & bitMask);
480                symbBitPos--;
481                break;
482             }
483             case UL_SLOT:
484             {
485                /*symbol BitMap to be set to 01 */
486                bitMask = 1<<symbBitPos;
487                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((0<<symbBitPos) & bitMask);
488                symbBitPos--;
489                bitMask = 1<<symbBitPos;
490                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((1<<symbBitPos) & bitMask);
491                symbBitPos--;
492                break;
493             }
494             case FLEXI_SLOT:
495             {
496                /*symbol BitMap to be set to 10 */
497                bitMask = 1<<symbBitPos;
498                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((1<<symbBitPos) & bitMask);
499                symbBitPos--;
500                bitMask = 1<<symbBitPos;
501                cell->symbFrmtBitMap = (cell->symbFrmtBitMap & ~(bitMask)) | ((0<<symbBitPos) & bitMask);
502                symbBitPos--;
503                break;
504             }
505             default:
506                DU_LOG("\nERROR  -->  SCH : Invalid slot Config in schInitTddSlotCfg");
507          }
508       }
509    }
510
511 }
512 #endif
513
514 /**
515  * @brief Fill SSB start symbol
516  *
517  * @details
518  *
519  *     Function : fillSsbStartSymb 
520  *      
521  *      This API stores SSB start index per beam
522  *           
523  *  @param[in]  SchCellCb     *cellCb
524  *  @return  int
525  *      -# ROK 
526  *      -# RFAILED 
527  **/
528 void fillSsbStartSymb(SchCellCb *cellCb)
529 {
530    uint8_t cnt, scs, symbIdx, ssbStartSymbArr[SCH_MAX_SSB_BEAM];
531
532    scs = cellCb->cellCfg.ssbSchCfg.scsCommon;
533
534    memset(ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
535    symbIdx = 0;
536    /* Determine value of "n" based on Section 4.1 of 3GPP TS 38.213 */
537    switch(scs)
538    {
539       case SCS_15KHZ:
540          {
541             if(cellCb->cellCfg.dlFreq <= 300000)
542                cnt = 2;/* n = 0, 1 */
543             else
544                cnt = 4; /* n = 0, 1, 2, 3 */
545             for(uint8_t idx=0; idx<cnt; idx++)
546             {
547                /* start symbol determined using {2, 8} + 14n */
548                ssbStartSymbArr[symbIdx++] = 2 + SCH_SYMBOL_PER_SLOT*idx;
549                ssbStartSymbArr[symbIdx++] = 8 + SCH_SYMBOL_PER_SLOT*idx;
550             }
551          }
552          break;
553       case SCS_30KHZ:
554          {
555             if(cellCb->cellCfg.dlFreq <= 300000)
556                cnt = 1;/* n = 0 */
557             else
558                cnt = 2; /* n = 0, 1 */
559             for(uint8_t idx=0; idx<cnt; idx++)
560             {
561                /* start symbol determined using {4, 8, 16, 20} + 28n */
562                ssbStartSymbArr[symbIdx++] = 4 + SCH_SYMBOL_PER_SLOT*idx;
563                ssbStartSymbArr[symbIdx++] = 8 + SCH_SYMBOL_PER_SLOT*idx;
564                ssbStartSymbArr[symbIdx++] = 16 + SCH_SYMBOL_PER_SLOT*idx;
565                ssbStartSymbArr[symbIdx++] = 20 + SCH_SYMBOL_PER_SLOT*idx;
566             }
567          }
568          break;
569       default:
570          DU_LOG("\nERROR  -->  SCH : SCS %d is currently not supported", scs);
571    }
572    memset(cellCb->ssbStartSymbArr, 0, sizeof(SCH_MAX_SSB_BEAM));
573    memcpy(cellCb->ssbStartSymbArr, ssbStartSymbArr, SCH_MAX_SSB_BEAM);
574
575 }
576
577
578 /**
579  * @brief init cellCb based on cellCfg
580  *
581  * @details
582  *
583  *     Function : schInitCellCb 
584  *      
585  *      This API is invoked after receiving schCellCfg
586  *           
587  *  @param[in]  schCellCb *cell
588  *  @param[in]  SchCellCfg *schCellCfg
589  *  @return  int
590  *      -# ROK 
591  *      -# RFAILED 
592  **/
593 uint8_t schInitCellCb(Inst inst, SchCellCfg *schCellCfg)
594 {
595    SchCellCb *cell= NULLP;
596    SCH_ALLOC(cell, sizeof(SchCellCb));
597    if(!cell)
598    {
599       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
600       return RFAILED;
601    }
602
603    cell->cellId = schCellCfg->cellId; 
604    cell->instIdx = inst;
605    switch(schCellCfg->numerology)
606    {
607       case SCH_NUMEROLOGY_0:
608          {
609             cell->numSlots = SCH_MU0_NUM_SLOTS;
610          }
611          break;
612       case SCH_NUMEROLOGY_1:
613          {
614             cell->numSlots = SCH_MU1_NUM_SLOTS;
615          }
616          break;
617       case SCH_NUMEROLOGY_2:
618          {
619             cell->numSlots = SCH_MU2_NUM_SLOTS;
620          }
621          break;
622       case SCH_NUMEROLOGY_3:
623          {
624             cell->numSlots = SCH_MU3_NUM_SLOTS;
625          }
626          break;
627       case SCH_NUMEROLOGY_4:
628          {
629             cell->numSlots = SCH_MU4_NUM_SLOTS;
630          }
631          break;
632       default:
633          DU_LOG("\nERROR  -->  SCH : Numerology %d not supported", schCellCfg->numerology);
634    }
635 #ifdef NR_TDD
636    schInitTddSlotCfg(cell, schCellCfg);   
637 #endif
638
639    SCH_ALLOC(cell->schDlSlotInfo, cell->numSlots * sizeof(SchDlSlotInfo*));
640    if(!cell->schDlSlotInfo)
641    {
642       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schDlSlotInfo");
643       return RFAILED;
644    }
645
646    SCH_ALLOC(cell->schUlSlotInfo, cell->numSlots * sizeof(SchUlSlotInfo*));
647    if(!cell->schUlSlotInfo)
648    {
649       DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb for schUlSlotInfo");
650       return RFAILED;
651    }
652
653    for(uint8_t idx=0; idx<cell->numSlots; idx++)
654    {
655       SchDlSlotInfo *schDlSlotInfo;
656       SchUlSlotInfo *schUlSlotInfo;
657
658       /* DL Alloc */
659       SCH_ALLOC(schDlSlotInfo, sizeof(SchDlSlotInfo));
660       if(!schDlSlotInfo)
661       {
662          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
663          return RFAILED;
664       }
665
666       /* UL Alloc */
667       SCH_ALLOC(schUlSlotInfo, sizeof(SchUlSlotInfo));
668       if(!schUlSlotInfo)
669       {
670          DU_LOG("\nERROR  -->  SCH : Memory allocation failed in schInitCellCb");
671          return RFAILED;
672       }
673
674       schInitDlSlot(schDlSlotInfo);
675       schInitUlSlot(schUlSlotInfo);
676
677       cell->schDlSlotInfo[idx] = schDlSlotInfo;
678       cell->schUlSlotInfo[idx] = schUlSlotInfo;
679
680    }
681    cell->firstSsbTransmitted = false;
682    cell->firstSib1Transmitted = false;
683    fillSsbStartSymb(cell);
684    schCb[inst].cells[inst] = cell;
685
686    DU_LOG("\nINFO  -->  SCH : Cell init completed for cellId:%d", cell->cellId);
687
688    return ROK;   
689 }
690
691 /**
692  * @brief Fill SIB1 configuration
693  *
694  * @details
695  *
696  *     Function : fillSchSib1Cfg
697  *
698  *     Fill SIB1 configuration
699  *
700  *  @param[in]  uint8_t bandwidth : total available bandwidth
701  *              uint8_t numSlots : total slots per SFN
702  *              SchSib1Cfg *sib1SchCfg : cfg to be filled
703  *              uint16_t pci : physical cell Id
704  *              uint8_t offsetPointA : offset
705  *  @return  void
706  **/
707 void fillSchSib1Cfg(uint8_t bandwidth, uint8_t numSlots, SchSib1Cfg *sib1SchCfg, uint16_t pci, uint8_t offsetPointA)
708 {
709    uint8_t coreset0Idx = 0;
710    uint8_t searchSpace0Idx = 0;
711    //uint8_t ssbMuxPattern = 0;
712    uint8_t numRbs = 0;
713    uint8_t numSymbols = 0;
714    uint8_t offset = 0;
715    uint8_t oValue = 0;
716    //uint8_t numSearchSpacePerSlot = 0;
717    uint8_t mValue = 0;
718    uint8_t firstSymbol = 0; /* need to calculate using formula mentioned in 38.213 */
719    uint8_t slotIndex = 0;
720    uint8_t FreqDomainResource[6] = {0};
721    uint16_t tbSize = 0;
722    uint8_t numPdschSymbols = 12; /* considering pdsch region from 2 to 13 */
723
724    PdcchCfg *pdcch = &(sib1SchCfg->sib1PdcchCfg);
725    PdschCfg *pdsch = &(sib1SchCfg->sib1PdschCfg);
726    BwpCfg *bwp = &(sib1SchCfg->bwp);
727
728    coreset0Idx     = sib1SchCfg->coresetZeroIndex;
729    searchSpace0Idx = sib1SchCfg->searchSpaceZeroIndex;
730
731    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
732    //ssbMuxPattern = coresetIdxTable[coreset0Idx][0];
733    numRbs        = coresetIdxTable[coreset0Idx][1];
734    numSymbols    = coresetIdxTable[coreset0Idx][2];
735    offset        = coresetIdxTable[coreset0Idx][3];
736
737    /* derive the search space params from table 13-11 spec 38.213 */
738    oValue                = searchSpaceIdxTable[searchSpace0Idx][0];
739    //numSearchSpacePerSlot = searchSpaceIdxTable[searchSpace0Idx][1];
740    mValue                = searchSpaceIdxTable[searchSpace0Idx][2];
741    firstSymbol           = searchSpaceIdxTable[searchSpace0Idx][3];
742
743    /* calculate the n0, need to add the formulae, as of now the value is 0 
744     * Need to add the even and odd values of i during configuration 
745     * [(O . 2^u + i . M )  ] mod numSlotsPerSubframe 
746     * assuming u = 0, i = 0, numSlotsPerSubframe = 10
747     * Also, from this configuration, coreset0 is only on even subframe */
748    slotIndex = ((oValue * 1) + (0 * mValue)) % numSlots; 
749    sib1SchCfg->n0 = slotIndex;
750
751    /* calculate the PRBs */
752    freqDomRscAllocType0(((offsetPointA-offset)/6), (numRbs/6), FreqDomainResource);
753
754    /* fill BWP */
755    switch(bandwidth)
756    {
757       case BANDWIDTH_20MHZ:
758          {
759             bwp->freqAlloc.numPrb = TOTAL_PRB_20MHZ_MU0;
760          }
761          break;
762       case BANDWIDTH_100MHZ:
763          {
764             bwp->freqAlloc.numPrb = TOTAL_PRB_100MHZ_MU1;
765          }
766          break;
767       default:
768          DU_LOG("\nERROR  -->  SCH : Bandwidth %d not supported", bandwidth);
769
770    }
771    bwp->freqAlloc.startPrb = 0;
772    bwp->subcarrierSpacing  = 0;         /* 15Khz */
773    bwp->cyclicPrefix       = 0;              /* normal */
774
775    /* fill the PDCCH PDU */
776    pdcch->coresetCfg.coreSetSize = numRbs;
777    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
778    pdcch->coresetCfg.durationSymbols = numSymbols;
779    memcpy(pdcch->coresetCfg.freqDomainResource,FreqDomainResource,6);
780    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
781    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
782    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
783    pdcch->coresetCfg.coreSetType = 0;
784    pdcch->coresetCfg.shiftIndex = pci;
785    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
786    pdcch->numDlDci = 1;
787    pdcch->dci.rnti = SI_RNTI;
788    pdcch->dci.scramblingId = pci;
789    pdcch->dci.scramblingRnti = 0;
790    pdcch->dci.cceIndex = 0;
791    pdcch->dci.aggregLevel = 4;
792    pdcch->dci.beamPdcchInfo.numPrgs = 1;
793    pdcch->dci.beamPdcchInfo.prgSize = 1;
794    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
795    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
796    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
797    pdcch->dci.txPdcchPower.powerValue = 0;
798    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
799    /* Storing pdschCfg pointer here. Required to access pdsch config while
800       fillig up pdcch pdu */
801    pdcch->dci.pdschCfg = pdsch; 
802
803    /* fill the PDSCH PDU */
804    uint8_t cwCount = 0;
805    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
806    pdsch->rnti = 0xFFFF; /* SI-RNTI */
807    pdsch->pduIndex = 0;
808    pdsch->numCodewords = 1;
809    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
810    {
811       pdsch->codeword[cwCount].targetCodeRate = 308;
812       pdsch->codeword[cwCount].qamModOrder = 2;
813       pdsch->codeword[cwCount].mcsIndex = sib1SchCfg->sib1Mcs;
814       pdsch->codeword[cwCount].mcsTable = 0; /* notqam256 */
815       pdsch->codeword[cwCount].rvIndex = 0;
816       tbSize = schCalcTbSize(sib1SchCfg->sib1PduLen);
817       pdsch->codeword[cwCount].tbSize = tbSize;
818    }
819    pdsch->dataScramblingId                   = pci;
820    pdsch->numLayers                          = 1;
821    pdsch->transmissionScheme                 = 0;
822    pdsch->refPoint                           = 0;
823    pdsch->dmrs.dlDmrsSymbPos                 = 2;
824    pdsch->dmrs.dmrsConfigType                = 0; /* type-1 */
825    pdsch->dmrs.dlDmrsScramblingId            = pci;
826    pdsch->dmrs.scid                          = 0;
827    pdsch->dmrs.numDmrsCdmGrpsNoData          = 1;
828    pdsch->dmrs.dmrsPorts                     = 0;
829    pdsch->dmrs.mappingType                   = DMRS_MAP_TYPE_A; /* Type-A */
830    pdsch->dmrs.nrOfDmrsSymbols               = NUM_DMRS_SYMBOLS;
831    pdsch->dmrs.dmrsAddPos                    = DMRS_ADDITIONAL_POS;
832
833    pdsch->pdschFreqAlloc.resourceAllocType   = 1; /* RAT type-1 RIV format */
834    pdsch->pdschFreqAlloc.freqAlloc.startPrb  = offsetPointA + SCH_SSB_NUM_PRB + 1; /* the RB numbering starts from coreset0,
835                                                                             and PDSCH is always above SSB */
836    pdsch->pdschFreqAlloc.freqAlloc.numPrb    = schCalcNumPrb(tbSize,sib1SchCfg->sib1Mcs,numPdschSymbols);
837    pdsch->pdschFreqAlloc.vrbPrbMapping       = 0; /* non-interleaved */
838    pdsch->pdschTimeAlloc.rowIndex            = 1;
839    pdsch->pdschTimeAlloc.timeAlloc.startSymb = 2; /* spec-38.214, Table 5.1.2.1-1 */
840    pdsch->pdschTimeAlloc.timeAlloc.numSymb   = numPdschSymbols;
841    pdsch->beamPdschInfo.numPrgs              = 1;
842    pdsch->beamPdschInfo.prgSize              = 1;
843    pdsch->beamPdschInfo.digBfInterfaces      = 0;
844    pdsch->beamPdschInfo.prg[0].pmIdx         = 0;
845    pdsch->beamPdschInfo.prg[0].beamIdx[0]    = 0;
846    pdsch->txPdschPower.powerControlOffset    = 0;
847    pdsch->txPdschPower.powerControlOffsetSS  = 0;
848
849 }
850
851 /**
852  * @brief cell config from MAC to SCH.
853  *
854  * @details
855  *
856  *     Function : macSchCellCfgReq
857  *      
858  *      This API is invoked by MAC to send cell config to SCH
859  *           
860  *  @param[in]  Pst            *pst
861  *  @param[in]  SchCellCfg     *schCellCfg
862  *  @return  int
863  *      -# ROK 
864  *      -# RFAILED 
865  **/
866 uint8_t SchHdlCellCfgReq(Pst *pst, SchCellCfg *schCellCfg)
867 {
868    uint8_t ret = ROK;
869    SchCellCb *cellCb;
870    SchCellCfgCfm schCellCfgCfm;
871    Pst rspPst;
872    Inst inst = pst->dstInst-1; 
873
874    schInitCellCb(inst, schCellCfg);
875    cellCb = schCb[inst].cells[inst]; //cells is of MAX_CELLS, why inst
876    cellCb->macInst = pst->srcInst;
877
878    /* derive the SIB1 config parameters */
879    fillSchSib1Cfg(schCellCfg->bandwidth, cellCb->numSlots,
880          &(schCellCfg->sib1SchCfg), schCellCfg->phyCellId,
881          schCellCfg->ssbSchCfg.ssbOffsetPointA);
882    memcpy(&cellCb->cellCfg, schCellCfg, sizeof(SchCellCfg));
883
884    /* Initializing global variables */
885    cellCb->actvUeBitMap = 0;
886    cellCb->boIndBitMap = 0;
887
888    /* Fill and send Cell config confirm */
889    memset(&rspPst, 0, sizeof(Pst));
890    FILL_PST_SCH_TO_MAC(rspPst, pst->dstInst);
891    rspPst.event = EVENT_SCH_CELL_CFG_CFM;
892
893    schCellCfgCfm.cellId = schCellCfg->cellId; 
894    schCellCfgCfm.rsp = RSP_OK;
895
896    ret = (*SchCellCfgCfmOpts[rspPst.selector])(&rspPst, &schCellCfgCfm);
897
898    return ret;
899
900 }
901
902 /*******************************************************************
903  *
904  * @brief Processes DL RLC BO info from MAC
905  *
906  * @details
907  *
908  *    Function : MacSchDlRlcBoInfo
909  *
910  *    Functionality:
911  *       Processes DL RLC BO info from MAC
912  *
913  * @params[in] 
914  * @return ROK     - success
915  *         RFAILED - failure
916  *
917  * ****************************************************************/
918 uint8_t MacSchDlRlcBoInfo(Pst *pst, DlRlcBoInfo *dlBoInfo)
919 {
920    uint8_t  lcId = 0;
921    uint16_t ueIdx = 0;
922    uint16_t slot;
923    SchUeCb *ueCb = NULLP;
924    SchCellCb *cell = NULLP;
925    SchDlSlotInfo *schDlSlotInfo = NULLP;
926
927    Inst  inst = pst->dstInst-SCH_INST_START;
928    DU_LOG("\nDEBUG  -->  SCH : Received RLC BO Status indication");
929    cell = schCb[inst].cells[inst];
930
931    GET_UE_IDX(dlBoInfo->crnti, ueIdx);
932    ueCb = &cell->ueCb[ueIdx-1];
933    lcId  = dlBoInfo->lcId;
934
935    if(lcId == SRB1_LCID || lcId == SRB2_LCID || lcId == SRB3_LCID || \
936       (lcId >= MIN_DRB_LCID && lcId <= MAX_DRB_LCID))
937    {
938       SET_ONE_BIT(ueIdx, cell->boIndBitMap);
939       ueCb->dlInfo.dlLcCtxt[lcId].bo = dlBoInfo->dataVolume;
940    }
941    else if(lcId != SRB0_LCID)
942    {
943       DU_LOG("\nERROR  -->  SCH : Invalid LC Id %d in MacSchDlRlcBoInfo", lcId);
944       return RFAILED;
945    }
946
947    slot = (cell->slotInfo.slot + SCHED_DELTA + PHY_DELTA_DL + BO_DELTA) % cell->numSlots;
948    schDlSlotInfo = cell->schDlSlotInfo[slot];
949
950    SCH_ALLOC(schDlSlotInfo->dlMsgInfo, sizeof(DlMsgInfo));
951    if(!schDlSlotInfo->dlMsgInfo)
952    {
953       DU_LOG("\nERROR  -->  SCH : Memory allocation failed for dlMsgInfo");
954       schDlSlotInfo = NULL;
955       return RFAILED;
956    }
957    schDlSlotInfo->dlMsgInfo->crnti = dlBoInfo->crnti;
958    schDlSlotInfo->dlMsgInfo->ndi = 1;
959    schDlSlotInfo->dlMsgInfo->harqProcNum = 0;
960    schDlSlotInfo->dlMsgInfo->dlAssignIdx = 0;
961    schDlSlotInfo->dlMsgInfo->pucchTpc = 0;
962    schDlSlotInfo->dlMsgInfo->pucchResInd = 0;
963    schDlSlotInfo->dlMsgInfo->harqFeedbackInd = 0;
964    schDlSlotInfo->dlMsgInfo->dciFormatId = 1;
965    if(lcId == SRB0_LCID)
966       schDlSlotInfo->dlMsgInfo->isMsg4Pdu = true;
967
968    return ROK;
969 }
970
971 /*******************************************************************
972  *
973  * @brief Processes BSR indiation from MAC
974  *
975  * @details
976  *
977  *    Function : MacSchBsr
978  *
979  *    Functionality:
980  *       Processes DL BSR from MAC
981  *
982  * @params[in]    Pst pst
983  *                UlBufferStatusRptInd bsrInd
984  * @return ROK     - success
985  *         RFAILED - failure
986  *
987  * ****************************************************************/
988 uint8_t MacSchBsr(Pst *pst, UlBufferStatusRptInd *bsrInd)
989 {
990    Inst           schInst       = pst->dstInst-SCH_INST_START;
991    SchCellCb      *cellCb       = NULLP;
992    SchUeCb        *ueCb         = NULLP;
993    uint8_t        lcgIdx;
994
995    DU_LOG("\nDEBUG  -->  SCH : Received BSR");
996    cellCb = schCb[schInst].cells[schInst];
997    ueCb = schGetUeCb(cellCb, bsrInd->crnti);
998
999    /* store dataVolume per lcg in uecb */
1000    for(lcgIdx = 0; lcgIdx < bsrInd->numLcg; lcgIdx++)
1001    {
1002       ueCb->bsrInfo[lcgIdx].priority = 1; //TODO: determining LCG priority?
1003       ueCb->bsrInfo[lcgIdx].dataVol = bsrInd->dataVolInfo[lcgIdx].dataVol;
1004    }
1005    return ROK;
1006 }
1007
1008 /*******************************************************************
1009  *
1010  * @brief Processes SR UCI indication from MAC 
1011  *
1012  * @details
1013  *
1014  *    Function : MacSchSrUciInd
1015  *
1016  *    Functionality:
1017  *      Processes SR UCI indication from MAC
1018  *
1019  * @params[in] Post structure
1020  *             UCI Indication
1021  * @return ROK     - success
1022  *         RFAILED - failure
1023  *
1024  * ****************************************************************/
1025 uint8_t MacSchSrUciInd(Pst *pst, SrUciIndInfo *uciInd)
1026 {
1027    Inst  inst = pst->dstInst-SCH_INST_START;
1028
1029    SchUeCb   *ueCb; 
1030    SchCellCb *cellCb = schCb[inst].cells[inst];
1031
1032    DU_LOG("\nDEBUG  -->  SCH : Received SR");
1033
1034    ueCb = schGetUeCb(cellCb, uciInd->crnti);
1035
1036    if(uciInd->numSrBits)
1037    {
1038       ueCb->srRcvd = true;
1039    }
1040    return ROK;
1041 }
1042 /**********************************************************************
1043   End of file
1044  **********************************************************************/