Building mib PDU and SSB changes at scheduler
[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 "stdbool.h"
35 #include "envopt.h"        /* environment options */
36 #include "envdep.h"        /* environment dependent */
37 #include "envind.h"        /* environment independent */
38 #include "gen.h"           /* general layer */
39 #include "ssi.h"           /* system service interface */
40 #include "cm_tkns.h"       /* Common Token Defines */
41 #include "cm_llist.h"      /* Common Link List Defines */
42 #include "cm_hash.h"       /* Common Hash List Defines */
43 #include "cm_mblk.h"       /* common memory link list library */
44 #include "cm_lte.h"        /* Common LTE Defines */
45 #include "lrg.h"
46 #include "rgr.h"
47 #include "tfu.h"
48 #include "rg_sch_inf.h"
49 #include "rg_sch.h"
50 #include "gen.x"           /* general layer typedefs */
51 #include "ssi.x"           /* system services typedefs */
52 #include "cm5.x"           /* system services */
53 #include "cm_tkns.x"       /* Common Token Definitions */
54 #include "cm_llist.x"      /* Common Link List Definitions */
55 #include "cm_lib.x"        /* Common Library Definitions */
56 #include "cm_hash.x"       /* Common Hash List Definitions */
57 #include "cm_mblk.x"       /* common memory link list library */
58 #include "cm_lte.x"        /* Common LTE Defines */
59 #include "tfu.x"           /* TFU types */
60 #include "lrg.x"           /* layer management typedefs for MAC */
61 #include "rgr.x"           /* layer management typedefs for MAC */
62 #include "rg_sch_inf.x"         /* typedefs for Scheduler */
63 #include "du_app_mac_inf.h"
64 #include "mac_sch_interface.h"
65 #include "sch.h"
66 #include "du_log.h"
67
68 extern SchCb schCb[SCH_MAX_INST];
69 /* local defines */
70 SchCellCfgCfmFunc SchCellCfgCfmOpts[] = 
71 {
72         packSchCellCfgCfm,     /* LC */
73         MacProcSchCellCfgCfm,  /* TC */
74         packSchCellCfgCfm      /* LWLC */
75 };
76
77 /**
78  * @brief Task Initiation function. 
79  *
80  * @details
81  *
82  *     Function : schActvInit
83  *     
84  *     This function is supplied as one of parameters during MAC's 
85  *     task registration. MAC will invoke this function once, after
86  *     it creates and attaches this TAPA Task to a system task.
87  *     
88  *  @param[in]  Ent Entity, the entity ID of this task.     
89  *  @param[in]  Inst Inst, the instance ID of this task.
90  *  @param[in]  Region Region, the region ID registered for memory 
91  *              usage of this task.
92  *  @param[in]  Reason Reason.
93  *  @return  int
94  *      -# ROK
95  **/
96 int schActvInit
97 (
98 Ent    entity,            /* entity */
99 Inst   instId,             /* instance */
100 Region region,         /* region */
101 Reason reason          /* reason */
102 )
103 {
104    Inst inst = (instId  - SCH_INST_START);
105
106    /* Initialize the MAC TskInit structure to zero */
107    cmMemset ((uint8_t *)&schCb[inst], 0, sizeof(schCb));
108
109    /* Initialize the MAC TskInit with received values */
110    schCb[inst].schInit.ent = entity;
111    schCb[inst].schInit.inst = inst;
112    schCb[inst].schInit.region = region;
113    schCb[inst].schInit.pool = 0;
114    schCb[inst].schInit.reason = reason;
115    schCb[inst].schInit.cfgDone = FALSE;
116    schCb[inst].schInit.acnt = FALSE;
117    schCb[inst].schInit.usta = FALSE;
118    schCb[inst].schInit.trc = FALSE;
119    schCb[inst].schInit.procId = SFndProcId();
120
121    RETVALUE(ROK);
122 } /* schActvInit */
123
124 /**
125  * @brief Scheduler instance Configuration Handler. 
126  *
127  * @details
128  *
129  *     Function : SchInstCfg
130  *     
131  *     This function in called by HandleSchGenCfgReq(). It handles the
132  *     general configurations of the scheduler instance. Returns
133  *     reason for success/failure of this function.
134  *     
135  *  @param[in]  RgCfg *cfg, the Configuaration information 
136  *  @return  U16
137  *      -# LCM_REASON_NOT_APPL 
138  *      -# LCM_REASON_INVALID_MSGTYPE
139  *      -# LCM_REASON_MEM_NOAVAIL
140  **/
141 PUBLIC U16 SchInstCfg
142 (
143 RgCfg *cfg,            /* Configuaration information */
144 Inst  dInst
145 )
146 {
147    uint16_t ret = LCM_REASON_NOT_APPL;
148    Inst     inst = (dInst - SCH_INST_START);
149
150    printf("\nEntered SchInstCfg()");
151    /* Check if Instance Configuration is done already */
152    if (schCb[inst].schInit.cfgDone == TRUE)
153    {
154       RETVALUE(LCM_REASON_INVALID_MSGTYPE);
155    }
156    /* Update the Pst structure for LM interface */
157    cmMemcpy((U8 *)&schCb[inst].schInit.lmPst,
158             (U8 *)&cfg->s.schInstCfg.genCfg.lmPst,
159              sizeof(Pst));
160    
161    schCb[inst].schInit.inst = inst;
162    schCb[inst].schInit.lmPst.srcProcId = schCb[inst].schInit.procId;
163    schCb[inst].schInit.lmPst.srcEnt = schCb[inst].schInit.ent;
164    schCb[inst].schInit.lmPst.srcInst = schCb[inst].schInit.inst +
165    SCH_INST_START;
166    schCb[inst].schInit.lmPst.event = EVTNONE;
167
168    schCb[inst].schInit.region = cfg->s.schInstCfg.genCfg.mem.region;
169    schCb[inst].schInit.pool = cfg->s.schInstCfg.genCfg.mem.pool;
170    schCb[inst].genCfg.tmrRes = cfg->s.schInstCfg.genCfg.tmrRes;
171 #ifdef LTE_ADV
172    schCb[inst].genCfg.forceCntrlSrbBoOnPCel =  cfg->s.schInstCfg.genCfg.forceCntrlSrbBoOnPCel;
173    schCb[inst].genCfg.isSCellActDeactAlgoEnable =  cfg->s.schInstCfg.genCfg.isSCellActDeactAlgoEnable;
174 #endif
175    schCb[inst].genCfg.startCellId    = cfg->s.schInstCfg.genCfg.startCellId;
176 #if 0
177    /* Initialzie the timer queue */   
178    cmMemset((U8 *)&schCb[inst].tmrTq, 0, sizeof(CmTqType)*RGSCH_TQ_SIZE);
179    /* Initialize the timer control point */
180    cmMemset((U8 *)&schCb[inst].tmrTqCp, 0, sizeof(CmTqCp));
181    schCb[inst].tmrTqCp.tmrLen = RGSCH_TQ_SIZE;
182
183    /* SS_MT_TMR needs to be enabled as schActvTmr needs instance information */
184    /* Timer Registration request to SSI */
185    if (SRegTmrMt(schCb[inst].schInit.ent, dInst,
186       (S16)schCb[inst].genCfg.tmrRes, schActvTmr) != ROK)
187    {
188       RLOG_ARG0(L_ERROR,DBG_INSTID,inst, "SchInstCfg(): Failed to "
189              "register timer.");
190       RETVALUE(LCM_REASON_MEM_NOAVAIL);
191    }   
192 #endif               
193    /* Set Config done in TskInit */
194    schCb[inst].schInit.cfgDone = TRUE;
195    printf("\nScheduler gen config done");
196    
197    RETVALUE(ret);
198 }
199
200 /**
201  * @brief Layer Manager Configuration request handler. 
202  *
203  * @details
204  *
205  *     Function : HandleSchGenCfgReq
206  *     
207  *     This function handles the configuration
208  *     request received at scheduler instance from the Layer Manager.
209  *     -# Based on the cfg->hdr.elmId.elmnt value it invokes one of the
210  *        functions rgHdlGenCfg() or rgHdlSapCfg().
211  *     -# Invokes RgMiLrgSchCfgCfm() to send back the confirmation to the LM.
212  *     
213  *  @param[in]  Pst *pst, the post structure     
214  *  @param[in]  RgMngmt *cfg, the configuration parameter's structure
215  *  @return  S16
216  *      -# ROK
217  **/
218 int HandleSchGenCfgReq
219 (
220 Pst      *pst,    /* post structure  */
221 RgMngmt  *cfg     /* config structure  */
222 )
223 {
224    uint16_t       ret = LCM_PRIM_OK;
225    uint16_t       reason = LCM_REASON_NOT_APPL;
226    RgMngmt   cfm;
227    Pst       cfmPst;
228
229    if(pst->dstInst < SCH_INST_START)
230    {
231       DU_LOG("\nInvalid inst ID");
232       DU_LOG("\nHandleSchGenCfgReq(): "
233                 "pst->dstInst=%d SCH_INST_START=%d", pst->dstInst,SCH_INST_START); 
234       RETVALUE(ROK);
235    }
236    printf("\nReceived scheduler gen config");
237    /* Fill the post structure for sending the confirmation */
238         memset(&cfmPst, 0 , sizeof(Pst));
239    SchFillCfmPst(pst, &cfmPst, cfg);
240
241    cmMemset((U8 *)&cfm, 0, sizeof(RgMngmt));
242
243 #ifdef LMINT3
244    cfm.hdr.transId =
245       cfg->hdr.transId;
246 #endif
247
248    cfm.hdr.elmId.elmnt = cfg->hdr.elmId.elmnt;
249    switch(cfg->hdr.elmId.elmnt)
250    {
251       case STSCHINST:
252          reason = SchInstCfg(&cfg->t.cfg,pst->dstInst );
253          break;
254       default:
255          ret = LCM_PRIM_NOK;
256          reason = LCM_REASON_INVALID_ELMNT;
257          DU_LOG("\nInvalid Elmnt=%d", cfg->hdr.elmId.elmnt);
258          break;
259    }
260
261    if (reason != LCM_REASON_NOT_APPL)
262    {
263       ret = LCM_PRIM_NOK;
264    }
265
266    cfm.cfm.status = ret;
267    cfm.cfm.reason = reason;
268
269    SchSendCfgCfm(&cfmPst, &cfm);
270    /*   SPutSBuf(pst->region, pst->pool, (Data *)cfg, sizeof(RgMngmt)); */
271    
272    RETVALUE(ROK);
273 }/*-- HandleSchGenCfgReq --*/
274
275 /**
276  * @brief slot indication from MAC to SCH.
277  *
278  * @details
279  *
280  *     Function : macSchSlotInd 
281  *      
282  *      This API is invoked by PHY to indicate slot indication to Scheduler for
283  *      a cell.
284  *           
285  *  @param[in]  Pst            *pst
286  *  @param[in]  SlotIndInfo    *slotInd
287  *  @return  S16
288  *      -# ROK 
289  *      -# RFAILED 
290  **/
291 int macSchSlotInd 
292 (
293 Pst                 *pst, 
294 SlotIndInfo         *slotInd
295 )
296 {
297    Inst  inst = pst->dstInst-SCH_INST_START;
298
299    /* Now call the TOM (Tfu ownership module) primitive to process further */
300    schProcessSlotInd(slotInd, inst);
301
302    RETVALUE(ROK);
303 }  /* macSchSlotInd */
304
305 /**
306  * @brief inti cellCb based on cellCfg
307  *
308  * @details
309  *
310  *     Function : InitSchCellCb 
311  *      
312  *      This API is invoked after receiving schCellCfg
313  *           
314  *  @param[in]  schCellCb *cell
315  *  @param[in]  SchCellCfg *schCellCfg
316  *  @return  int
317  *      -# ROK 
318  *      -# RFAILED 
319  **/
320 int InitSchCellCb(Inst inst, SchCellCfg *schCellCfg)
321 {
322    SchCellCb *cell;
323    SCH_ALLOC(cell, sizeof(SchCellCb));
324         if(!cell)
325         {
326       DU_LOG("\nMemory allocation failed in InitSchCellCb");
327                 return RFAILED;
328         }
329
330         cell->cellId = schCellCfg->cellId; 
331         cell->instIdx = inst;
332         switch(schCellCfg->scsCommon)
333         {
334            case SCH_SCS_15KHZ:
335                 {
336                         cell->numSlots = SCH_NUM_SLOTS;
337                 }
338                 break;
339                 default:
340                    DU_LOG("\nSCS %d not supported", schCellCfg->scsCommon);
341         }
342   
343    for(uint8_t idx=0; idx<SCH_NUM_SLOTS; idx++)
344         {
345                 SchDlAlloc *schDlAlloc;
346                 SCH_ALLOC(schDlAlloc, sizeof(SchDlAlloc));
347                 if(!schDlAlloc)
348                 {
349                         DU_LOG("\nMemory allocation failed in InitSchCellCb");
350                         return RFAILED;
351                 }
352
353                 schDlAlloc->totalPrb = ((schCellCfg->bandwidth *
354                                 1000)/(schCellCfg->scsCommon))/SCH_NUM_SC_PRB;
355                 for(uint8_t itr=0; itr<MAX_SSB_IDX; itr++)
356                 {
357                         schDlAlloc->assignedPrb[itr] = 0;
358                 }
359                 for(uint8_t itr=0; itr<MAX_SSB_IDX; itr++)
360                 {
361                         memset(&schDlAlloc->ssbInfo[itr], 0, sizeof(SsbInfo));
362                 }
363
364                 cell->dlAlloc[idx] = schDlAlloc;
365         }
366         schCb[inst].cells[inst] = cell; //Sphoorthi TODO: check if this works
367
368    DU_LOG("\nCell init completed for cellId:%d", cell->cellId);
369
370    return ROK;   
371 }
372
373 /**
374  * @brief cell config from MAC to SCH.
375  *
376  * @details
377  *
378  *     Function : macSchCellCfgReq
379  *      
380  *      This API is invoked by MAC to send cell config to SCH
381  *           
382  *  @param[in]  Pst            *pst
383  *  @param[in]  SchCellCfg     *schCellCfg
384  *  @return  int
385  *      -# ROK 
386  *      -# RFAILED 
387  **/
388 int SchHdlCellCfgReq
389 (
390 Pst                 *pst, 
391 SchCellCfg          *schCellCfg
392 )
393 {
394    int ret = ROK;
395    SchCellCb *cellCb;
396         SchCellCfgCfm schCellCfgCfm;
397         Pst rspPst;
398         Inst inst = pst->dstInst-1; 
399
400         InitSchCellCb(inst, schCellCfg);
401         cellCb = schCb[inst].cells[inst];
402    cellCb->macInst = pst->srcInst;
403    memcpy(&cellCb->cellCfg, schCellCfg, sizeof(SchCellCfg));
404
405    memset(&rspPst, 0, sizeof(Pst));
406    SCH_FILL_RSP_PST(rspPst, inst);
407         rspPst.event = EVENT_SCH_CELL_CFG_CFM;
408         schCellCfgCfm.rsp = RSP_OK;
409   
410    ret = (*SchCellCfgCfmOpts[rspPst.selector])(&rspPst, &schCellCfgCfm);
411
412    return ret;
413
414 }
415
416 /**********************************************************************
417          End of file
418 **********************************************************************/