[Epic-ID: ODUHIGH-406][Task-ID: ODUHIGH-415]: Paging Configuration
[o-du/l2.git] / src / 5gnrmac / mac_cfg_hdl.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 /* This file stores handler for MAC and SCH configuration requests */
19 /* header include files (.h) */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "lrg.x"
23 #include "du_app_mac_inf.h"
24 #include "mac_sch_interface.h"
25 #include "lwr_mac_upr_inf.h"
26 #include "mac.h"
27 #include "rlc_mac_inf.h"
28 #include "mac_upr_inf_api.h"
29 #include "lwr_mac.h"
30 #include "lwr_mac_fsm.h"
31 #include "mac_utils.h"
32 #include "lwr_mac_phy.h"
33
34 uint8_t ssbPeriodicity[6] = {5, 10, 20, 40, 80, 160};
35
36 uint8_t MacSchCellCfgReq(Pst *pst, MacCellCfg  *macCellCfg);
37
38 packMacCellCfgConfirm packMacCellCfmOpts[] =
39 {
40    packMacCellCfgCfm,      /* packing for loosely coupled */
41    duHandleMacCellCfgCfm,      /* packing for tightly coupled */
42    packMacCellCfgCfm,    /* packing for light weight loosly coupled */
43 };
44
45 SchCellCfgFunc SchCellCfgOpts[] =
46 {
47    packSchCellCfg,   /* packing for loosely coupled */
48    SchHdlCellCfgReq, /* packing for tightly coupled */
49    packSchCellCfg    /* packing for light weight loosly coupled */
50 };
51
52 MacSchCellDeleteReqFunc macSchCellDeleteReqOpts[]=
53 {
54    packMacSchCellDeleteReq,    /* packing for loosely coupled */
55    MacSchCellDeleteReq,        /* packing for tightly coupled */
56    packMacSchCellDeleteReq     /* packing for light weight loosely coupled */
57 };
58
59 MacDuCellDeleteRspFunc macDuCellDeleteRspOpts[] =
60 {
61    packDuMacCellDeleteRsp,   /* packing for loosely coupled */
62    DuProcMacCellDeleteRsp,   /* packing for tightly coupled */
63    packDuMacCellDeleteRsp   /* packing for light weight loosly coupled */
64 };
65
66 MacDuSliceCfgRspFunc macDuSliceCfgRspOpts[] =
67 {
68    packDuMacSliceCfgRsp,   /* packing for loosely coupled */
69    DuProcMacSliceCfgRsp,   /* packing for tightly coupled */
70    packDuMacSliceCfgRsp   /* packing for light weight loosly coupled */
71 };
72
73 MacDuSliceReCfgRspFunc macDuSliceReCfgRspOpts[] =
74 {
75    packDuMacSliceReCfgRsp,   /* packing for loosely coupled */
76    DuProcMacSliceReCfgRsp,   /* packing for tightly coupled */
77    packDuMacSliceReCfgRsp   /* packing for light weight loosly coupled */
78 };
79
80 /**
81  * @brief Layer Manager  Configuration request handler for Scheduler
82  *
83  * @details
84  *
85  *     Function : MacSchGenCfgReq
86  *
87  *     This function receives general configurations for Scheduler
88  *     from DU APP and forwards to Scheduler.
89  *
90  *  @param[in]  Pst *pst, the post structure
91  *  @param[in]  RgMngmt *cfg, the configuration parameter's structure
92  *  @return 
93  *      -# ROK
94  **/
95 uint8_t MacSchGenCfgReq(Pst *pst, RgMngmt *cfg)
96 {
97    printf("\nReceived Scheduler gen config at MAC");
98    pst->dstInst = DEFAULT_CELLS + 1;
99    SchProcGenCfgReq(pst, cfg);
100
101    return ROK;
102 }
103
104 /**
105  * @brief Layer Manager Configuration response from Scheduler
106  *
107  * @details
108  *
109  *     Function : SchSendCfgCfm
110  *
111  *     This function sends general configurations response from
112  *     Scheduler to DU APP.
113  *
114  *  @param[in]  Pst *pst, the post structure
115  *  @param[in]  RgMngmt *cfm, the configuration confirm structure
116  *  @return 
117  *      -# ROK
118  **/
119 uint8_t SchSendCfgCfm(Pst *pst, RgMngmt  *cfm)
120 {
121    DU_LOG("\nDEBUG  -->  Sending Scheduler config confirm to DU APP");
122    pst->dstEnt = ENTDUAPP;
123    pst->dstInst = 0;
124    pst->srcInst = 0;
125    pst->selector = ODU_SELECTOR_LC;
126    RgMiLrgSchCfgCfm(pst, cfm);
127
128    return ROK;
129 }
130
131 /**
132  * @brief Layer Manager Configuration request handler.
133  *
134  * @details
135  *
136  *     Function : MacProcCellCfgReq
137  *
138  *     This function handles the gNB and cell configuration
139  *     request received from DU APP.
140  *     This API unapcks and forwards the config towards SCH
141  *
142  *  @param[in]  Pst           *pst
143  *  @param[in]  MacCellCfg    *macCellCfg
144  *  @return  
145  *      -# ROK
146  **/
147 uint8_t MacProcCellCfgReq(Pst *pst, MacCellCfg *macCellCfg)
148 {
149    Pst cfmPst;
150    uint16_t cellIdx;
151    uint8_t ret = ROK, sliceIdx = 0;
152    MacCellCb     *macCellCb;
153
154    memset((uint8_t *)&cfmPst, 0, sizeof(Pst));
155
156    MAC_ALLOC(macCellCb, sizeof(MacCellCb));
157    if(macCellCb == NULLP)
158    {
159       DU_LOG("\nERROR  -->  MAC : macCellCb is NULL at handling of macCellCfg\n");
160       return RFAILED;
161    }
162    memset(macCellCb, 0, sizeof(MacCellCb));
163
164    GET_CELL_IDX(macCellCfg->cellId, cellIdx);
165    macCb.macCell[cellIdx] = macCellCb;
166    macCb.macCell[cellIdx]->cellId = macCellCfg->cellId;
167    memcpy(&macCb.macCell[cellIdx]->macCellCfg, macCellCfg, sizeof(MacCellCfg));
168
169    MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1Pdu, \
170          macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1PduLen);
171    if(macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1Pdu == NULLP)
172    {
173       DU_LOG("\nERROR  -->  MAC : macCellCb is NULL at handling of sib1Pdu of macCellCfg\n");
174       return RFAILED;
175    }
176    memcpy(macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1Pdu, macCellCfg->sib1Cfg.sib1Pdu, \
177          macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1PduLen);
178    
179    macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.numSupportedSlice = macCellCfg->plmnInfoList.numSupportedSlice;
180    MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai, macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.numSupportedSlice\
181          * sizeof(Snssai*));
182    if(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai == NULLP)
183    {
184       DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacProcCellCfgReq");
185       return RFAILED;
186    }
187
188    if(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai)
189    {
190       for(sliceIdx=0; sliceIdx<macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.numSupportedSlice; sliceIdx++)
191       {
192          if(macCellCfg->plmnInfoList.snssai[sliceIdx])
193          {
194             MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai[sliceIdx], sizeof(Snssai));
195             if(!macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai[sliceIdx])
196             {
197                DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacProcCellCfgReq");
198                return RFAILED;
199             }
200             memcpy(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai[sliceIdx], macCellCfg->plmnInfoList.snssai[sliceIdx],\
201             sizeof(Snssai));
202          }
203       }
204    }
205
206    /* Send cell cfg to scheduler */
207    ret = MacSchCellCfgReq(pst, macCellCfg);
208    if(ret != ROK)
209    {
210       MacCellCfgCfm macCellCfgCfm;
211       macCellCfgCfm.rsp = RSP_NOK;
212       macCellCfgCfm.cellId = macCellCfg->cellId;
213       /* Fill Pst */
214       FILL_PST_MAC_TO_DUAPP(cfmPst, EVENT_MAC_CELL_CONFIG_CFM);
215       cfmPst.selector  = ODU_SELECTOR_LC;
216
217       ret = (*packMacCellCfmOpts[cfmPst.selector])(&cfmPst,&macCellCfgCfm);
218    }
219    else
220    {
221       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macCellCfg ,sizeof(MacCellCfg));
222    }
223    return ret;
224 } /* end of MacProcCellCfgReq */
225
226 /**
227  * @brief Layer Manager Configuration request handler.
228  *
229  * @details
230  *
231  *     Function : MacSchCellCfgReq
232  *
233  *     This function sends cell configuration to SCH
234  *
235  *  @param[in]  Pst           *pst
236  *  @param[in]  MacCellCfg    *macCellCfg
237  *  @return  
238  *      -# ROK
239  **/
240 uint8_t MacSchCellCfgReq(Pst *pst, MacCellCfg *macCellCfg)
241 {
242    SchCellCfg schCellCfg;
243    Pst        cfgPst;
244    uint8_t    ssbMaskIdx = 0, rsrcListIdx = 0, ret=0, sliceIdx=0;
245
246    memset(&cfgPst, 0, sizeof(Pst));
247    memset(&schCellCfg, 0, sizeof(SchCellCfg));
248    schCellCfg.cellId = macCellCfg->cellId;
249    schCellCfg.phyCellId = macCellCfg->phyCellId;
250    schCellCfg.numerology = macCellCfg->numerology;
251    schCellCfg.dupMode = macCellCfg->dupType;
252    schCellCfg.bandwidth = macCellCfg->dlCarrCfg.bw;
253    schCellCfg.dlFreq = macCellCfg->dlCarrCfg.freq;
254    schCellCfg.ulFreq = macCellCfg->ulCarrCfg.freq;
255
256    /* fill ssb scheduler parameters */
257    schCellCfg.ssbSchCfg.ssbPbchPwr = macCellCfg->ssbCfg.ssbPbchPwr;
258    schCellCfg.ssbSchCfg.scsCommon = macCellCfg->ssbCfg.scsCmn;
259    schCellCfg.ssbSchCfg.ssbOffsetPointA = macCellCfg->ssbCfg.ssbOffsetPointA;
260    schCellCfg.ssbSchCfg.ssbPeriod = ssbPeriodicity[macCellCfg->ssbCfg.ssbPeriod];
261    schCellCfg.ssbSchCfg.ssbSubcOffset = macCellCfg->ssbCfg.ssbScOffset;
262    for(ssbMaskIdx=0; ssbMaskIdx<SSB_MASK_SIZE; ssbMaskIdx++)
263    {
264       schCellCfg.ssbSchCfg.nSSBMask[ssbMaskIdx] = macCellCfg->ssbCfg.ssbMask[ssbMaskIdx];
265    }
266    schCellCfg.ssbSchCfg.totNumSsb = countSetBits(schCellCfg.ssbSchCfg.nSSBMask[0]);
267
268    /* fill SIB1 scheduler parameters */
269    schCellCfg.sib1SchCfg.sib1PduLen = macCellCfg->sib1Cfg.sib1PduLen;
270    schCellCfg.sib1SchCfg.sib1RepetitionPeriod = macCellCfg->sib1Cfg.sib1RepetitionPeriod;
271    schCellCfg.sib1SchCfg.coresetZeroIndex = macCellCfg->sib1Cfg.coresetZeroIndex;
272    schCellCfg.sib1SchCfg.searchSpaceZeroIndex = macCellCfg->sib1Cfg.searchSpaceZeroIndex;
273    schCellCfg.sib1SchCfg.sib1Mcs = macCellCfg->sib1Cfg.sib1Mcs;
274    schCellCfg.sib1SchCfg.pageCfg.numPO  =  macCellCfg->sib1Cfg.pagingCfg.numPO;
275    schCellCfg.sib1SchCfg.pageCfg.poPresent = macCellCfg->sib1Cfg.pagingCfg.poPresent;
276
277    if(schCellCfg.sib1SchCfg.pageCfg.poPresent)
278    {
279       memcpy(schCellCfg.sib1SchCfg.pageCfg.pagingOcc, macCellCfg->sib1Cfg.pagingCfg.pagingOcc, MAX_PO_PER_PF);
280    }
281
282    /* fill RACH config params */
283    schCellCfg.schRachCfg.prachCfgIdx = macCellCfg->prachCfg.prachCfgIdx;
284    schCellCfg.schRachCfg.prachSubcSpacing = \
285                                             macCellCfg->prachCfg.prachSubcSpacing;
286    schCellCfg.schRachCfg.msg1FreqStart = macCellCfg->prachCfg.msg1FreqStart;
287    schCellCfg.schRachCfg.msg1Fdm       = macCellCfg->prachCfg.msg1Fdm;
288    schCellCfg.schRachCfg.rootSeqLen    = macCellCfg->prachCfg.rootSeqLen;
289    schCellCfg.schRachCfg.rootSeqIdx    = macCellCfg->prachCfg.fdm[0].rootSeqIdx;
290    schCellCfg.schRachCfg.numRootSeq    = macCellCfg->prachCfg.fdm[0].numRootSeq;
291    schCellCfg.schRachCfg.k1            = macCellCfg->prachCfg.fdm[0].k1;
292    schCellCfg.schRachCfg.ssbPerRach    = macCellCfg->prachCfg.ssbPerRach;
293    schCellCfg.schRachCfg.prachMultCarrBand = \
294                                              macCellCfg->prachCfg.prachMultCarrBand;
295    schCellCfg.schRachCfg.raContResTmr  = macCellCfg->prachCfg.raContResTmr;
296    schCellCfg.schRachCfg.rsrpThreshSsb = macCellCfg->prachCfg.rsrpThreshSsb;
297    schCellCfg.schRachCfg.raRspWindow   = macCellCfg->prachCfg.raRspWindow;
298
299    /* fill initial DL BWP */
300    schCellCfg.schInitialDlBwp.bwp.freqAlloc.startPrb = macCellCfg->initialDlBwp.bwp.firstPrb;
301    schCellCfg.schInitialDlBwp.bwp.freqAlloc.numPrb = macCellCfg->initialDlBwp.bwp.numPrb;
302    schCellCfg.schInitialDlBwp.bwp.scs = macCellCfg->initialDlBwp.bwp.scs;
303    schCellCfg.schInitialDlBwp.bwp.cyclicPrefix = macCellCfg->initialDlBwp.bwp.cyclicPrefix;
304    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.searchSpaceId =
305       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.searchSpaceId;
306    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.coresetId =
307       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.coresetId;
308    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.monitoringSlot =
309       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.monitoringSlot;
310    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.duration =
311       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.duration;
312    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.monitoringSymbol =
313       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.monitoringSymbol;
314    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel1 =
315       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel1;
316    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel2 =
317       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel2;
318    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel4 =
319       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel4;
320    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel8 =
321       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel8;
322    schCellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel16 =
323       macCellCfg->initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel16;
324    schCellCfg.schInitialDlBwp.pdschCommon.numTimeDomAlloc = macCellCfg->initialDlBwp.pdschCommon.numTimeDomAlloc;
325    for(rsrcListIdx = 0; rsrcListIdx<macCellCfg->initialDlBwp.pdschCommon.numTimeDomAlloc; rsrcListIdx++)
326    {
327       schCellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].k0 = 
328          macCellCfg->initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].k0;
329       schCellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType =
330          macCellCfg->initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType;
331       schCellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol =
332          macCellCfg->initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol;
333       schCellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].lengthSymbol =
334          macCellCfg->initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].lengthSymbol;
335    }
336
337    /* fill initial UL BWP */
338    schCellCfg.schInitialUlBwp.bwp.freqAlloc.startPrb = macCellCfg->initialUlBwp.bwp.firstPrb;
339    schCellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb = macCellCfg->initialUlBwp.bwp.numPrb;
340    schCellCfg.schInitialUlBwp.bwp.scs = macCellCfg->initialUlBwp.bwp.scs;
341    schCellCfg.schInitialUlBwp.bwp.cyclicPrefix = macCellCfg->initialUlBwp.bwp.cyclicPrefix;
342    schCellCfg.schInitialUlBwp.puschCommon.numTimeDomRsrcAlloc = \
343       macCellCfg->initialUlBwp.puschCommon.numTimeDomRsrcAlloc;
344    for(rsrcListIdx = 0; rsrcListIdx < macCellCfg->initialUlBwp.puschCommon.numTimeDomRsrcAlloc; rsrcListIdx++)
345    {
346       schCellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].k2 = 
347          macCellCfg->initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].k2;
348       schCellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType =
349          macCellCfg->initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType;
350       schCellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol =
351          macCellCfg->initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol;
352       schCellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].symbolLength =
353          macCellCfg->initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].symbolLength;
354    }
355
356    if(macCellCfg->plmnInfoList.snssai) 
357    {
358       schCellCfg.plmnInfoList.numSliceSupport = macCellCfg->plmnInfoList.numSupportedSlice;
359       MAC_ALLOC(schCellCfg.plmnInfoList.snssai, schCellCfg.plmnInfoList.numSliceSupport * sizeof(Snssai*));
360       if(!schCellCfg.plmnInfoList.snssai)
361       {
362          DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacSchCellCfgReq");
363          return RFAILED;
364       }
365       for(sliceIdx=0; sliceIdx<schCellCfg.plmnInfoList.numSliceSupport; sliceIdx++)
366       {
367          if(macCellCfg->plmnInfoList.snssai[sliceIdx])
368          {
369             MAC_ALLOC(schCellCfg.plmnInfoList.snssai[sliceIdx], sizeof(Snssai));
370             if(!schCellCfg.plmnInfoList.snssai[sliceIdx])
371             {
372                DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacSchCellCfgReq");
373                return RFAILED;
374             }
375             memcpy(schCellCfg.plmnInfoList.snssai[sliceIdx], macCellCfg->plmnInfoList.snssai[sliceIdx],  sizeof(Snssai));
376          }
377       }
378    }
379
380 #ifdef NR_TDD
381    memcpy(&schCellCfg.tddCfg, &macCellCfg->tddCfg, sizeof(TDDCfg));
382 #endif
383
384    FILL_PST_MAC_TO_SCH(cfgPst, EVENT_SCH_CELL_CFG);
385
386    ret = (*SchCellCfgOpts[cfgPst.selector])(&cfgPst, &schCellCfg);
387    return ret;
388 } /* end of MacSchCellCfgReq */
389
390
391 /*******************************************************************
392  *
393  * @brief Sends Cell config confirm to DU APP
394  *
395  * @details
396  *
397  *    Function : MacSendCellCfgCfm
398  *
399  *    Functionality:
400  *      Sends Cell config confirm to DU APP
401  *
402  * @params[in] Response status
403  * @return void
404  *
405  * ****************************************************************/
406 void MacSendCellCfgCfm(uint16_t cellId, uint8_t response)
407 {
408    Pst pst;
409    uint16_t   cellIdx;
410    MacCellCfgCfm macCellCfgCfm;
411
412    memset(&pst, 0, sizeof(Pst));
413
414    GET_CELL_IDX(cellId, cellIdx);
415    macCellCfgCfm.cellId = macCb.macCell[cellIdx]->macCellCfg.cellId;
416    macCellCfgCfm.rsp = response;
417
418    /* Fill Pst */
419    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_CELL_CONFIG_CFM);
420    pst.selector  = ODU_SELECTOR_LC;
421
422    (*packMacCellCfmOpts[pst.selector])(&pst,&macCellCfgCfm);
423 }
424
425
426 /**
427  * @brief Layer Manager Configuration response handler.
428  *
429  * @details
430  *
431  *     Function : MacProcSchCellCfgCfm
432  *
433  *     This function processes cell configuration to SCH
434  *
435  *  @param[in]  Pst           *pst
436  *  @param[in]  SchCellCfgCfm *schCellCfgCfm
437  *  @return  int
438  *      -# ROK
439  **/
440 uint8_t MacProcSchCellCfgCfm(Pst *pst, SchCellCfgCfm *schCellCfgCfm)
441 {
442    uint16_t *cellId = NULLP;
443
444 #ifdef CALL_FLOW_DEBUG_LOG
445    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_SCH_CELL_CFG_CFM\n");
446 #endif
447
448    if(schCellCfgCfm->rsp == RSP_OK)
449    {
450       cellId = &schCellCfgCfm->cellId;
451 #ifdef INTEL_TIMER_MODE
452       sendToLowerMac(UL_IQ_SAMPLE, 0, (void *)cellId);
453 #else
454       sendToLowerMac(CONFIG_REQUEST, 0, (void *)cellId);
455 #endif
456    }
457    else
458    {
459       MacSendCellCfgCfm(schCellCfgCfm->cellId, RSP_NOK);
460    }
461    return ROK;
462 }
463
464 /*******************************************************************
465  *
466  * @brief MAC handler for config response from PHY
467  *
468  * @details
469  *
470  *    Function : fapiMacConfigRsp
471  *
472  *    Functionality:
473  *     Processes config response from PHY and sends cell config
474  *     confirm to DU APP
475  *
476  * @params[in]
477  * @return void
478  *
479  * ****************************************************************/
480 void fapiMacConfigRsp(uint16_t cellId)
481 {
482    /* TODO : Processing of config response from PHY */
483
484    /* Send cell config cfm to DU APP */
485    MacSendCellCfgCfm(cellId, RSP_OK);
486 }
487
488 /*******************************************************************
489  *
490  * @brief Fill and Send Cell Delete response from MAC to DU APP
491  *
492  * @details
493  *
494  *    Function : MacSendCellDeleteRsp
495  *
496  *    Functionality: Fill and Send Cell Delete response from MAC to DUAPP
497  *
498  * @params[in] MAC Cell delete result
499  *             SCH Cell delete response
500  * @return ROK     - success
501  *         RFAILED - failure
502  *
503  * ****************************************************************/
504 uint8_t MacSendCellDeleteRsp(CellDeleteStatus result, uint8_t cellId)
505 {
506    MacCellDeleteRsp *deleteRsp=NULLP;
507    Pst            rspPst;
508
509    MAC_ALLOC_SHRABL_BUF(deleteRsp, sizeof(MacCellDeleteRsp));
510    if(!deleteRsp)
511    {
512       DU_LOG("\nERROR  -->  MAC : MacSendCellDeleteRsp(): Memory allocation for Cell delete response failed");
513       return RFAILED;
514    }
515
516    /* Filling CELL delete response */
517    
518    memset(deleteRsp, 0, sizeof(MacCellDeleteRsp));
519    deleteRsp->cellId = cellId;
520    deleteRsp->result = result;
521
522    /* Fill Post structure and send CELL delete response*/
523    memset(&rspPst, 0, sizeof(Pst));
524    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_CELL_DELETE_RSP);
525    return (*macDuCellDeleteRspOpts[rspPst.selector])(&rspPst, deleteRsp);
526 }
527
528 /*******************************************************************
529  *
530  * @brief  Processes CELL delete response from scheduler
531  *
532  * @details
533  *
534  *    Function : MacProcSchCellDeleteRsp
535  *
536  *    Functionality:
537  *      Processes CELL delete from scheduler
538  *
539  * @params[in] Pst : Post structure
540  *             schCellDelRsp : Scheduler CELL delete respons
541  * @return ROK     - success
542  *         RFAILED - failure
543  *
544  * * ****************************************************************/
545 uint8_t MacProcSchCellDeleteRsp(Pst *pst, SchCellDeleteRsp *schCellDelRsp)
546 {
547    uint8_t  ret = ROK, sliceIdx = 0;
548    uint16_t cellIdx=0;
549    CellDeleteStatus status;
550    
551 #ifdef CALL_FLOW_DEBUG_LOG
552    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_CELL_DELETE_RSP_TO_MAC\n");
553 #endif  
554
555    if(schCellDelRsp)
556    {
557       if(schCellDelRsp->rsp == RSP_OK)
558       {
559          DU_LOG("\nINFO   -->  MAC : SCH CELL Delete response for cellId[%d] is successful ", \
560          schCellDelRsp->cellId);
561          GET_CELL_IDX(schCellDelRsp->cellId, cellIdx);
562          if(macCb.macCell[cellIdx])
563          {
564             if(macCb.macCell[cellIdx]->cellId == schCellDelRsp->cellId)
565             {
566                status  = SUCCESSFUL_RSP;
567                if(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai)
568                {
569                   for(sliceIdx = 0; sliceIdx<macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.numSupportedSlice; sliceIdx++)
570                   {
571                      MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai[sliceIdx], sizeof(Snssai));
572                   }
573                   MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.snssai, macCb.macCell[cellIdx]->macCellCfg.plmnInfoList.\
574                   numSupportedSlice * sizeof(Snssai*));
575                }
576                MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1Pdu, \
577                   macCb.macCell[cellIdx]->macCellCfg.sib1Cfg.sib1PduLen);
578                MAC_FREE(macCb.macCell[cellIdx], sizeof(MacCellCb));
579             }
580             else
581             {
582                 DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
583                 status = CELL_ID_INVALID;
584                 ret = RFAILED;
585             }
586          }
587          else
588          {
589             DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
590             status = CELL_ID_INVALID;
591             ret = RFAILED;
592          }
593       }
594       else
595       {
596          DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
597          status = CELL_ID_INVALID;
598          ret = RFAILED;
599       }
600       if(MacSendCellDeleteRsp(status, schCellDelRsp->cellId) != ROK)
601       {
602          DU_LOG("\nERROR  -->  MAC: MacProcSchCellDeleteRsp(): Failed to send CELL delete response");
603          ret = RFAILED;
604       }
605
606    }
607    else
608    {
609       DU_LOG("\nERROR  -->  MAC: MacProcSchCellDeleteRsp(): schCellDelRsp is NULL");
610       ret = RFAILED;
611    }
612    return ret;
613 }
614
615 /*******************************************************************
616  *
617  * @brief Sends Cell delete req to Scheduler
618  *
619  * @details
620  *
621  *    Function : sendCellDelReqToSch
622  *
623  *    Functionality: sends Cell delete req to Scheduler
624  *
625  * @params[in]  SchCellDelete *schCellDel
626  * @return ROK     - success
627  *         RFAILED - failure
628  *
629  * ****************************************************************/
630
631 uint8_t sendCellDelReqToSch(SchCellDelete *schCellDel)
632 {
633    Pst schPst;
634    FILL_PST_MAC_TO_SCH(schPst, EVENT_CELL_DELETE_REQ_TO_SCH);
635    return(*macSchCellDeleteReqOpts[schPst.selector])(&schPst, schCellDel);
636 }
637
638 /*******************************************************************
639  *
640  * @brief Handles CELL Delete requst from DU APP
641  *
642  * @details
643  *
644  *    Function : MacProcCellDeleteReq
645  *
646  *    Functionality: Handles CELL Delete requst from DU APP
647  *
648  * @params[in] Pst *pst, MacCellDelete *cellDelete
649  * @return ROK     - success
650  *         RFAILED - failure
651  *
652  *
653  * ****************************************************************/
654 uint8_t MacProcCellDeleteReq(Pst *pst, MacCellDelete *cellDelete)
655 {
656    uint8_t ret = ROK, cellIdx=0;
657    SchCellDelete schCellDelete;
658
659    DU_LOG("\nINFO   -->  MAC : Cell Delete Request received for cellId[%d]", cellDelete->cellId);
660
661    if(cellDelete)
662    {
663       GET_CELL_IDX(cellDelete->cellId, cellIdx);
664       if(macCb.macCell[cellIdx])
665       {
666          if(macCb.macCell[cellIdx]->cellId == cellDelete->cellId)
667          {
668             memset(&schCellDelete, 0, sizeof(SchCellDelete));
669             schCellDelete.cellId =  cellDelete->cellId;
670             ret = sendCellDelReqToSch(&schCellDelete);
671             if(ret != ROK)
672             {
673                DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to send UE Delete Request to SCH");
674                ret = RFAILED;
675             }
676          }
677          else
678          {
679             DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to find the MacUeCb of CellId = %d",\
680             cellDelete->cellId);
681             ret = RFAILED;
682          }
683       }
684       else
685       {
686          DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to find the MacUeCb of CellId = %d",\
687                cellDelete->cellId);
688          ret = RFAILED;
689       }
690
691       if(ret == RFAILED)
692       {
693           DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Sending failure response to DU");
694           if(MacSendCellDeleteRsp(CELL_ID_INVALID, cellDelete->cellId) != ROK)
695           {
696              DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): failed to send cell delete rsp for cellID[%d]",\
697              cellDelete->cellId);
698           }
699
700       }
701       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellDelete, sizeof(MacCellDelete));
702    }
703    else
704    {
705       DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Received MacCellDelete is NULL");
706       ret = RFAILED;
707    }
708    return ret;
709 }
710
711 /**
712  * @brief free the temporary slice cfg stored in macCb.
713  *
714  * @details
715  *
716  *     Function : freeMacSliceCfgReq 
717  *
718  *   free the temporary slice cfg stored in macCb
719  *
720  *  @param[in]  
721  *  @return  int
722  *      -# ROK
723  **/
724 void freeMacSliceCfgReq(MacSliceCfgReq *cfgReq,Pst *pst)
725 {
726    uint8_t cfgIdx = 0;
727    
728    if(cfgReq)
729    {
730       if(cfgReq->numOfConfiguredSlice)
731       {
732          for(cfgIdx = 0; cfgIdx<cfgReq->numOfConfiguredSlice; cfgIdx++)
733          {
734             if(cfgReq->listOfSliceCfg[cfgIdx])
735             {
736                MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfSliceCfg[cfgIdx]->rrmPolicyRatio, sizeof(RrmPolicyRatio)); 
737             }
738             MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfSliceCfg[cfgIdx], sizeof(MacSliceRrmPolicy));
739          }
740          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfSliceCfg, cfgReq->numOfConfiguredSlice * sizeof(MacSliceRrmPolicy*));
741       }
742       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq, sizeof(MacSliceCfgReq));
743    }
744 }
745 /**
746  * @brief fill Mac Slice Config Rsp
747  *
748  * @details
749  *
750  *     Function : fillMacSliceCfgRsp 
751  *
752  *     This function   fill Mac Slice Config Rsp
753  *
754  *  @param[in]  SchSliceCfgRsp *sliceCfgRsp, MacSliceCfgRsp *macSliceCfgRsp,
755  *  uint8_t *count
756  *  @return  int
757  *      -# ROK
758  **/
759 uint8_t fillMacSliceCfgRsp(SchSliceCfgRsp *schSliceCfgRsp, MacSliceCfgRsp *macSliceCfgRsp)
760 {
761    
762     bool sliceFound = false;
763     uint8_t cfgIdx = 0;
764
765     macSliceCfgRsp->numSliceCfgRsp  = schSliceCfgRsp->numSliceCfgRsp;
766     MAC_ALLOC_SHRABL_BUF(macSliceCfgRsp->listOfSliceCfgRsp,  macSliceCfgRsp->numSliceCfgRsp* sizeof(MacSliceRsp*));
767     if(macSliceCfgRsp->listOfSliceCfgRsp == NULLP)
768     {
769        DU_LOG("\nERROR  -->  MAC : Memory allocation failedi in fillMacSliceCfgRsp");
770        return RFAILED;
771     }
772
773     for(cfgIdx = 0; cfgIdx<schSliceCfgRsp->numSliceCfgRsp; cfgIdx++)
774     {
775        sliceFound = false;
776        if(schSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->rsp == RSP_OK)
777        {
778           sliceFound = true;
779        }
780
781        MAC_ALLOC_SHRABL_BUF(macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx], sizeof(SliceRsp));
782        if(macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx] == NULLP)
783        {
784           DU_LOG("\nERROR  -->  MAC : Memory allocation failedi in fillMacSliceCfgRsp");
785           return RFAILED;
786        }
787
788        macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->snssai = schSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->snssai;
789        if(sliceFound == true)
790           macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->rsp    = MAC_DU_APP_RSP_OK;
791        else
792        {
793           macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->rsp    = MAC_DU_APP_RSP_NOK;
794           macSliceCfgRsp->listOfSliceCfgRsp[cfgIdx]->cause  = SLICE_NOT_PRESENT;
795        }
796     }
797     return ROK;
798 }
799
800 /**
801  * @brief send slice cfg response to duapp.
802  *
803  * @details
804  *
805  *     Function : MacSendSliceConfigRsp
806  *
807  *   sends  slice cfg response to duapp
808  *
809  *  @param[in] MacSliceCfgRsp macSliceCfgRsp 
810  *  @return  int
811  *      -# ROK
812  **/
813 uint8_t MacSendSliceConfigRsp(MacSliceCfgRsp *macSliceCfgRsp)
814 {
815     Pst  rspPst;
816     
817     memset(&rspPst, 0, sizeof(Pst));
818     FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_SLICE_CFG_RSP);
819     return (*macDuSliceCfgRspOpts[rspPst.selector])(&rspPst, macSliceCfgRsp);
820
821 }
822 /**
823  * @brief free the slice cfg rsp received from sch.
824  *
825  * @details
826  *
827  *     Function : freeSchSliceCfgRsp 
828  *
829  *     This free the slice cfg rsp received from sch
830  *
831  *  @param[in]  SchSliceCfgRsp *sliceCfgrsp
832  *  @return  int
833  *      -# ROK
834  **/
835 void freeSchSliceCfgRsp(SchSliceCfgRsp *schSliceCfgRsp)
836 {
837    uint8_t cfgIdx = 0;
838
839    if(schSliceCfgRsp)
840    {
841       if(schSliceCfgRsp->numSliceCfgRsp)
842       {
843          for(cfgIdx = 0; cfgIdx< schSliceCfgRsp->numSliceCfgRsp; cfgIdx++)
844          {
845             MAC_FREE(schSliceCfgRsp->listOfSliceCfgRsp[cfgIdx], sizeof(SliceRsp));
846          }
847          MAC_FREE(schSliceCfgRsp->listOfSliceCfgRsp, schSliceCfgRsp->numSliceCfgRsp * sizeof(SliceRsp*));
848       }
849    }
850 }
851
852 /**
853  * @brief Mac process the slice cfg rsp received from sch.
854  *
855  * @details
856  *
857  *     Function : MacProcSchSliceCfgRsp 
858  *
859  *     This function  process the slice cfg rsp received from sch
860  *
861  *  @param[in]  Pst           *pst
862  *  @param[in]  SchSliceCfgRsp *sliceCfgrsp
863  *  @return  int
864  *      -# ROK
865  **/
866 uint8_t MacProcSchSliceCfgRsp(Pst *pst, SchSliceCfgRsp *schSliceCfgRsp)
867 {
868    MacSliceCfgRsp *macSliceCfgRsp = NULLP;
869
870    if(schSliceCfgRsp)
871    {
872       MAC_ALLOC_SHRABL_BUF(macSliceCfgRsp, sizeof(MacSliceCfgRsp));
873       if(macSliceCfgRsp == NULLP)
874       {
875           DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchSliceCfgRsp");
876           return RFAILED;
877       }
878       if(schSliceCfgRsp->listOfSliceCfgRsp)
879       {
880          if(fillMacSliceCfgRsp(schSliceCfgRsp, macSliceCfgRsp) != ROK)
881          {
882             DU_LOG("\nERROR  -->  MAC : Failed to fill the slice cfg response");
883             return RFAILED;
884          }
885          MacSendSliceConfigRsp(macSliceCfgRsp);
886       }
887       freeSchSliceCfgRsp(schSliceCfgRsp);
888    }
889    return ROK;
890 }
891
892 /**
893 * @brief send slice cfg response to duapp.
894 *
895 * @details
896 *
897 *     Function : MacSendSliceReconfigRsp 
898 *
899 *   sends  slice cfg response to duapp
900 *
901 *  @param[in] MacSliceCfgRsp macSliceRecfgRsp
902 *  @return  int
903 *      -# ROK
904 **/
905 uint8_t MacSendSliceReconfigRsp(MacSliceCfgRsp *macSliceRecfgRsp)
906 {
907    Pst  rspPst;
908
909    memset(&rspPst, 0, sizeof(Pst));
910    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_SLICE_RECFG_RSP);
911    return (*macDuSliceReCfgRspOpts[rspPst.selector])(&rspPst, macSliceRecfgRsp);
912
913 }
914
915 /**
916  * @brief Mac process the slice cfg rsp received from sch.
917  *
918  * @details
919  *
920  *     Function : MacProcSchSliceReCfgRsp 
921  *
922  *     This function  process the slice cfg rsp received from sch
923  *
924  *  @param[in]  Pst           *pst
925  *  @param[in]  SchSliceCfgRsp *schSliceRecfgRsp
926  *  @return  int
927  *      -# ROK
928  **/
929 uint8_t MacProcSchSliceReCfgRsp(Pst *pst, SchSliceCfgRsp *schSliceRecfgRsp)
930 {
931    MacSliceCfgRsp *macSliceReCfgRsp = NULLP;
932
933    if(schSliceRecfgRsp)
934    {
935       MAC_ALLOC_SHRABL_BUF(macSliceReCfgRsp, sizeof(MacSliceCfgRsp));
936       if(macSliceReCfgRsp == NULLP)
937       {
938           DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchSliceReCfgRsp");
939           return RFAILED;
940       }
941
942       if(schSliceRecfgRsp->listOfSliceCfgRsp)
943       {
944          if(fillMacSliceCfgRsp(schSliceRecfgRsp, macSliceReCfgRsp) != ROK)
945          {
946             DU_LOG("\nERROR  -->  MAC : Failed to fill the slice Recfg response");
947             return RFAILED;
948          }
949          MacSendSliceReconfigRsp(macSliceReCfgRsp);
950       }
951       freeSchSliceCfgRsp(schSliceRecfgRsp);
952    }
953    return ROK;
954 }
955 /**********************************************************************
956   End of file
957  **********************************************************************/