PDSCH and PUSCH MCS Update [Issue-ID: ODUHIGH-261]
[o-du/l2.git] / src / 5gnrmac / mac_ue_mgr.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 /* This file contains UE management handling functionality for MAC */
20
21 /* header include files (.h) */
22 #include "common_def.h"
23 #include "lrg.h"
24 #include "lrg.x"
25 #include "du_app_mac_inf.h"
26 #include "mac_sch_interface.h"
27 #include "lwr_mac_upr_inf.h"
28 #include "mac.h"
29 #include "mac_utils.h"
30
31 /* function pointers for packing slot ind from mac to sch */
32 MacSchAddUeConfigReqFunc macSchAddUeConfigReqOpts[] =
33 {
34    packMacSchAddUeConfigReq,    /* packing for loosely coupled */
35    MacSchAddUeConfigReq,        /* packing for tightly coupled */
36    packMacSchAddUeConfigReq     /* packing for light weight loosely coupled */
37 };
38
39 MacDuUeCfgRspFunc macDuUeCfgRspOpts[] =
40 {
41    packDuMacUeCfgRsp,   /* packing for loosely coupled */
42    DuProcMacUeCfgRsp,   /* packing for tightly coupled */
43    packDuMacUeCfgRsp   /* packing for light weight loosly coupled */
44 };
45
46 MacSchModUeConfigReqFunc macSchModUeConfigReqOpts[] =
47 {
48    packMacSchModUeConfigReq,    /* packing for loosely coupled */
49    MacSchModUeConfigReq,        /* packing for tightly coupled */
50    packMacSchModUeConfigReq     /* packing for light weight loosely coupled */
51 };
52
53 /*******************************************************************
54  *
55  * @brief Fills mac cell group config to be sent to scheduler
56  *
57  * @details
58  *
59  *    Function : fillMacCellGroupCfg
60  *
61  *    Functionality: Fills mac cell group config to be sent to sch
62  *
63  * @params[in] macCellGrp : mac cell group config at MAC
64  *             *macCellGrpCfg : mac cell group cfg to be filled
65  * @return ROK     - success
66  *         RFAILED - failure
67  *
68  * ****************************************************************/
69 uint8_t fillMacCellGroupCfg(MacCellGrpCfg macCellGrp, SchMacCellGrpCfg  *macCellGrpCfg)
70 {
71    uint8_t           idx;
72    SchSchedReqCfg    *schedReqCfg;
73    SchTagCfg         *tagCfg;
74
75    /* Copy scheduling request config */
76    schedReqCfg = &macCellGrpCfg->schedReqCfg;
77    schedReqCfg->addModListCount = macCellGrp.schReqCfg.addModListCount;
78    if(schedReqCfg->addModListCount > MAX_NUM_SR_CFG_PER_CELL_GRP)
79    {
80       DU_LOG("\nMAC : Scheduling Request Add/Mod Count %d exceeds max limit %d", \
81             schedReqCfg->addModListCount, MAX_NUM_SR_CFG_PER_CELL_GRP);
82       return RFAILED;
83    }
84    for(idx = 0; idx < schedReqCfg->addModListCount; idx++)
85    {
86       schedReqCfg->addModList[idx].schedReqId = \
87          macCellGrp.schReqCfg.addModList[idx].schedReqId;
88       schedReqCfg->addModList[idx].srProhibitTmr = \
89          macCellGrp.schReqCfg.addModList[idx].srProhibitTmr;
90       schedReqCfg->addModList[idx].srTransMax = \
91          macCellGrp.schReqCfg.addModList[idx].srTransMax;
92    }
93    schedReqCfg->relListCount = macCellGrp.schReqCfg.relListCount;
94
95    /* copy TAG config */
96    tagCfg = &macCellGrpCfg->tagCfg;
97    tagCfg->addModListCount = macCellGrp.tagCfg.addModListCount;
98    if(tagCfg->addModListCount > MAX_NUM_TAGS)
99    {
100       DU_LOG("\nMAC : Scheduling Request Add/Mod Count %d exceeds max limit %d", \
101             tagCfg->addModListCount, MAX_NUM_TAGS);
102       return RFAILED;
103    }
104    for(idx = 0; idx < tagCfg->addModListCount; idx++)
105    {
106       tagCfg->addModList[idx].tagId = \
107          macCellGrp.tagCfg.addModList[idx].tagId;
108       tagCfg->addModList[idx].timeAlignmentTmr = \
109          macCellGrp.tagCfg.addModList[idx].timeAlignTimer;
110    }
111    tagCfg->relListCount = macCellGrp.tagCfg.relListCount;
112
113    /* Copy PHR config */
114    if(macCellGrp.phrCfgSetupPres)
115    {
116       macCellGrpCfg->phrCfg.periodicTmr = macCellGrp.phrCfg.periodicTimer;
117       macCellGrpCfg->phrCfg.prohibitTmr =  macCellGrp.phrCfg.prohibitTimer;
118       macCellGrpCfg->phrCfg.txpowerFactorChange = macCellGrp.phrCfg.txPowerFactor;
119       macCellGrpCfg->phrCfg.multiplePhr = macCellGrp.phrCfg.multiplePHR;
120       macCellGrpCfg->phrCfg.dummy = macCellGrp.phrCfg.dummy;
121       macCellGrpCfg->phrCfg.type2OtherCell = macCellGrp.phrCfg.phrType2OtherCell;
122       macCellGrpCfg->phrCfg.modeOtherCG = macCellGrp.phrCfg.phrOtherCG;
123    }
124
125    return ROK;
126 }
127
128 /*******************************************************************
129  *
130  * @brief Fills phy cell group config to be sent to scheduler
131  *
132  * @details
133  *
134  *    Function : fillPhyCellGroupCfg
135  *
136  *    Functionality: Fills phy cell group config to be sent to sch
137  *
138  * @params[in] macUeCfg : Phy cell group config at MAC
139  *             *schPhyCellGrp : phy cell group config to be filled
140  * @return ROK     - success
141  *         RFAILED - failure
142  *
143  * ****************************************************************/
144 uint8_t fillPhyCellGroupCfg(PhyCellGrpCfg macUeCfg, SchPhyCellGrpCfg *schPhyCellGrp)
145 {
146    schPhyCellGrp->pdschHarqAckCodebook = macUeCfg.pdschHarqAckCodebook;
147    schPhyCellGrp->pNrFr1 = macUeCfg.pNrFr1;
148
149    return ROK;
150 }
151
152 /*******************************************************************
153  *
154  * @brief Fills PDSCh serving cell config to send to scheduler
155  *
156  * @details
157  *
158  *    Function : fillPdschServCellCfg 
159  *
160  *    Functionality: Fills PDSCh serving cell config to send to scheduler
161  *
162  * @params[in] macPdschCfg : Pdsch serving cell config at MAC
163  *             *schPdschCfg : Pdsch serving cell config to be filled
164  * @return ROK     - success
165  *         RFAILED - failure
166  *
167  * ****************************************************************/
168 uint8_t fillPdschServCellCfg(PdschServCellCfg macPdschCfg, SchPdschServCellCfg *schPdschCfg) 
169 {
170    if(macPdschCfg.maxMimoLayers)
171    {
172       if(!schPdschCfg->maxMimoLayers)
173       {
174          MAC_ALLOC_SHRABL_BUF(schPdschCfg->maxMimoLayers, sizeof(uint8_t));
175          if(!schPdschCfg->maxMimoLayers)
176          {
177             DU_LOG("\nMAC :Memory Alloc MimoLayers Failed at fillPdschServCellCfg()");
178             return RFAILED;
179          }
180       }
181       *schPdschCfg->maxMimoLayers = *macPdschCfg.maxMimoLayers;
182    }
183    else
184    {
185       schPdschCfg->maxMimoLayers = NULLP;
186    }
187
188    schPdschCfg->numHarqProcForPdsch = \
189       macPdschCfg.numHarqProcForPdsch;
190
191    if(macPdschCfg.maxCodeBlkGrpPerTb)
192    {
193       if(!schPdschCfg->maxCodeBlkGrpPerTb)
194       {
195          MAC_ALLOC_SHRABL_BUF(schPdschCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
196          if(!schPdschCfg->maxCodeBlkGrpPerTb)
197          {
198             DU_LOG("\nMAC :Memory Alloc for code Block Failed at fillPdschServCellCfg()");
199             return RFAILED;
200          }
201       }
202       *schPdschCfg->maxCodeBlkGrpPerTb = *macPdschCfg.maxCodeBlkGrpPerTb;
203    }
204    else
205    {
206       schPdschCfg->maxCodeBlkGrpPerTb = NULLP;
207    }
208
209    if(macPdschCfg.codeBlkGrpFlushInd)
210    {
211       if(!schPdschCfg->codeBlkGrpFlushInd)
212       {
213          MAC_ALLOC_SHRABL_BUF(schPdschCfg->codeBlkGrpFlushInd, sizeof(bool));
214          if(!schPdschCfg->codeBlkGrpFlushInd)
215          {
216             DU_LOG("\nMAC :Memory Alloc for Flush Ind Failed at fillPdschServCellCfg()");
217             return RFAILED;
218          }
219       }
220       *schPdschCfg->codeBlkGrpFlushInd = *macPdschCfg.codeBlkGrpFlushInd;
221    }
222    else
223    {
224       schPdschCfg->codeBlkGrpFlushInd = NULLP;
225    }
226
227    if(macPdschCfg.xOverhead)
228    {
229       if(!schPdschCfg->xOverhead)
230       {
231          MAC_ALLOC_SHRABL_BUF(schPdschCfg->xOverhead, sizeof(SchPdschXOverhead));
232          if(!schPdschCfg->xOverhead)
233          {
234             DU_LOG("\nMAC :Memory Alloc for xOverHead Failed at fillPdschServCellCfg()");
235             return RFAILED;
236          }
237       }
238       *schPdschCfg->xOverhead = *macPdschCfg.xOverhead;
239    }
240    else
241    {
242       schPdschCfg->xOverhead = NULLP;
243    }
244
245    return ROK;
246 }
247
248 /*******************************************************************
249  *
250  * @brief Fills PUSCH cofig in initial UL BWP config for SCH UE Cfg
251  *
252  * @details
253  *
254  *    Function : fillInitalUlBwpPuschCfg
255  *
256  *    Functionality:
257  *       Fills PUSCH cofig in initial UL BWP config for SCH UE Cfg
258  *
259  * @params[in] macPuschCfg : Initial UL-BWP PUSCH cfg at MAC
260  *             schPuschCfg : Initial UL-BWP PUSCH cfg to be filled
261  * @return ROK     - success
262  *         RFAILED - failure
263  *
264  * ****************************************************************/
265 uint8_t fillInitalUlBwpPuschCfg(PuschCfg macPuschCfg, SchPuschCfg *schPuschCfg)
266 {
267    uint8_t   idx;
268
269    schPuschCfg->dataScramblingId = macPuschCfg.dataScramblingId;
270    schPuschCfg->dmrsUlCfgForPuschMapTypeA.addPos = \
271       macPuschCfg.dmrsUlCfgForPuschMapTypeA.addPos;
272    schPuschCfg->dmrsUlCfgForPuschMapTypeA.transPrecodDisabled.scramblingId0 = \
273       macPuschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled.scramblingId0;
274    schPuschCfg->resourceAllocType = macPuschCfg.resourceAllocType;
275
276    schPuschCfg->numTimeDomRsrcAlloc = macPuschCfg.numTimeDomRsrcAlloc;
277    if(schPuschCfg->numTimeDomRsrcAlloc > MAX_NUM_UL_ALLOC)
278    {
279       DU_LOG("\nMAC : Number of Time domain resource allocation [%d] exceeds max limit [%d]",\
280             schPuschCfg->numTimeDomRsrcAlloc, MAX_NUM_UL_ALLOC);
281       return RFAILED;
282    }    
283    for(idx = 0; idx < schPuschCfg->numTimeDomRsrcAlloc; idx++)
284    {
285       schPuschCfg->timeDomRsrcAllocList[idx].k2 = \
286          macPuschCfg.timeDomRsrcAllocList[idx].k2;
287       schPuschCfg->timeDomRsrcAllocList[idx].mappingType = \
288          macPuschCfg.timeDomRsrcAllocList[idx].mappingType;
289       schPuschCfg->timeDomRsrcAllocList[idx].startSymbol = \
290          macPuschCfg.timeDomRsrcAllocList[idx].startSymbol; 
291       schPuschCfg->timeDomRsrcAllocList[idx].symbolLength = \
292          macPuschCfg.timeDomRsrcAllocList[idx].symbolLength;
293    }
294
295    schPuschCfg->transformPrecoder = macPuschCfg.transformPrecoder;
296    return ROK;
297 }
298
299 /*******************************************************************
300  *
301  * @brief Fills initail UL BWP config to send to scheduler
302  *
303  * @details
304  *
305  *    Function : fillInitialUlBwp
306  *
307  *    Functionality: Fills initail UL BWP config to send to sch
308  *
309  * @params[in] macInitUlBwp : Initial UL BWP cfg at MAC
310  *             schInitUlBwp : Initial UL BWP cfg to be filled
311  * @return ROK     - success
312  *         RFAILED - failure
313  *
314  * ****************************************************************/
315 uint8_t fillInitialUlBwp(InitialUlBwp macInitUlBwp, SchInitialUlBwp *schInitUlBwp)
316 {
317    schInitUlBwp->pucchCfgPres = macInitUlBwp.pucchPresent;
318    if(schInitUlBwp->pucchCfgPres)
319    {
320       /* TODO : Optional parameter */
321    }
322
323    schInitUlBwp->puschCfgPres = macInitUlBwp.puschPresent;
324    if(schInitUlBwp->puschCfgPres)
325    {
326       if(fillInitalUlBwpPuschCfg(macInitUlBwp.puschCfg, &schInitUlBwp->puschCfg) != ROK)
327       {
328          DU_LOG("\nMAC : fillInitalUlBwpPuschCfg() failed");
329          return RFAILED;
330       }
331    }
332    return ROK;
333 }
334
335 /*******************************************************************
336  *
337  * @brief Fill PDCCH cfg in Initial DL BWP for UE Cfg in Scheduler
338  *
339  * @details
340  *
341  *    Function : fillInitDlBwpPdcchCfg
342  *
343  *    Functionality:
344  *        Fill PDCCH cfg in Initial DL BWP for UE Cfg in Scheduler
345  *
346  * @params[in] macPdcchCfg : Inital DL BWP PDCCH cfg in MAC
347  *             schPdcchCfg : Inital DL BWP PDCCH cfg to be filled
348  * @return ROK     - success
349  *         RFAILED - failure
350  *
351  * ****************************************************************/
352 uint8_t fillInitDlBwpPdcchCfg(PdcchConfig macPdcchCfg, SchPdcchConfig *schPdcchCfg)
353 {
354    uint8_t idx;
355
356    /* Fill CORESET info */
357    schPdcchCfg->numCRsetToAddMod = macPdcchCfg.numCRsetToAddMod;
358    if(schPdcchCfg->numCRsetToAddMod > MAX_NUM_CRSET)
359    {
360       DU_LOG("\nMAC : Number of CORESET to ADD/MOD [%d] exceeds max limit [%d]",\
361             schPdcchCfg->numCRsetToAddMod, MAX_NUM_CRSET);
362       return RFAILED;
363    }
364
365    for(idx = 0; idx < schPdcchCfg->numCRsetToAddMod; idx++)
366    {
367       schPdcchCfg->cRSetToAddModList[idx].cRSetId = \
368          macPdcchCfg.cRSetToAddModList[idx].cRSetId;
369       memcpy(&schPdcchCfg->cRSetToAddModList[idx].freqDomainRsrc,\
370          &macPdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
371       schPdcchCfg->cRSetToAddModList[idx].duration = \
372          macPdcchCfg.cRSetToAddModList[idx].duration;
373       schPdcchCfg->cRSetToAddModList[idx].cceRegMappingType = \
374          macPdcchCfg.cRSetToAddModList[idx].cceRegMappingType;
375       schPdcchCfg->cRSetToAddModList[idx].precoderGranularity = \
376          macPdcchCfg.cRSetToAddModList[idx].precoderGranularity;
377       schPdcchCfg->cRSetToAddModList[idx].dmrsScramblingId = \
378          macPdcchCfg.cRSetToAddModList[idx].dmrsScramblingId;
379    }
380
381    schPdcchCfg->numCRsetToRel = macPdcchCfg.numCRsetToRel;
382    if(schPdcchCfg->numCRsetToAddMod > MAX_NUM_CRSET)
383    {
384       DU_LOG("\nMAC : Number of CORESET to release [%d] exceeds max limit [%d]",\
385             schPdcchCfg->numCRsetToRel, MAX_NUM_CRSET);
386       return RFAILED;
387    }
388
389    for(idx = 0; idx < schPdcchCfg->numCRsetToRel; idx++)
390    {
391       /* TODO */
392    }
393
394    /* Fill Search space info */
395    schPdcchCfg->numSearchSpcToAddMod = macPdcchCfg.numSearchSpcToAddMod;
396    if(schPdcchCfg->numSearchSpcToAddMod > MAX_NUM_SEARCH_SPC)
397    {
398       DU_LOG("\nMAC : Number of search space to ADD/MOD [%d] exceeds max [%d]", \
399             schPdcchCfg->numSearchSpcToAddMod, MAX_NUM_SEARCH_SPC);
400       return RFAILED;
401    }
402    for(idx = 0; idx < schPdcchCfg->numSearchSpcToAddMod; idx++)
403    {
404       schPdcchCfg->searchSpcToAddModList[idx].searchSpaceId = \
405          macPdcchCfg.searchSpcToAddModList[idx].searchSpaceId;
406       schPdcchCfg->searchSpcToAddModList[idx].cRSetId = \
407          macPdcchCfg.searchSpcToAddModList[idx].cRSetId;
408       schPdcchCfg->searchSpcToAddModList[idx].mSlotPeriodicityAndOffset = \
409          macPdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset;
410       memcpy(&schPdcchCfg->searchSpcToAddModList[idx].mSymbolsWithinSlot,
411          &macPdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, \
412          MONITORING_SYMB_WITHIN_SLOT_SIZE);
413       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel1 = \
414          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1;
415       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel2 = \
416          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2;
417       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel4 = \
418          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4;
419       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel8 = \
420          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8;
421       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel16 = \
422          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16;
423       schPdcchCfg->searchSpcToAddModList[idx].searchSpaceType = \
424          macPdcchCfg.searchSpcToAddModList[idx].searchSpaceType;
425       schPdcchCfg->searchSpcToAddModList[idx].ueSpecificDciFormat = \
426          macPdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat;
427    }
428
429    schPdcchCfg->numSearchSpcToRel = macPdcchCfg.numSearchSpcToRel;
430    if(schPdcchCfg->numSearchSpcToRel > MAX_NUM_SEARCH_SPC)
431    {
432       DU_LOG("\nMAC : Number of search space to release [%d] exceeds max [%d]", \
433             schPdcchCfg->numSearchSpcToRel, MAX_NUM_SEARCH_SPC);
434       return RFAILED;
435    }
436    for(idx = 0; idx < schPdcchCfg->numSearchSpcToRel; idx++)
437    {
438       /* TODO */
439    }
440
441    return ROK;
442 }
443
444 /*******************************************************************
445  *
446  * @brief Fill PDSCH cfg in Initial DL BWP for UE Cfg in Scheduler
447  *
448  * @details
449  *
450  *    Function : fillInitDlBwpPdschCfg
451  *
452  *    Functionality:
453  *        Fill PDSCH cfg in Initial DL BWP for UE Cfg in Scheduler
454  *
455  * @params[in] macPdschCfg : Inital DL BWP PDSCH cfg at  MAC
456  *             schPdschCfg : Inital DL BWP PDSCH cfg to be filled
457  * @return ROK     - success
458  *         RFAILED - failure
459  *
460  * ****************************************************************/
461 uint8_t fillInitDlBwpPdschCfg(PdschConfig macPdschCfg, SchPdschConfig *schPdschCfg)
462 {
463    uint8_t   idx;
464
465    schPdschCfg->dmrsDlCfgForPdschMapTypeA.addPos = \
466        macPdschCfg.dmrsDlCfgForPdschMapTypeA.addPos;
467    schPdschCfg->resourceAllocType = macPdschCfg.resourceAllocType;
468    schPdschCfg->numTimeDomRsrcAlloc = macPdschCfg.numTimeDomRsrcAlloc;
469    if(schPdschCfg->numTimeDomRsrcAlloc > MAX_NUM_DL_ALLOC)
470    {
471       DU_LOG("\nMAC : Number of time domain resource allocation [%d], exceeds\
472             max limit [%d]", schPdschCfg->numTimeDomRsrcAlloc, MAX_NUM_DL_ALLOC);
473       return RFAILED;
474    }
475
476    for(idx = 0; idx < schPdschCfg->numTimeDomRsrcAlloc; idx++)
477    {
478       schPdschCfg->timeDomRsrcAllociList[idx].mappingType = \
479          macPdschCfg.timeDomRsrcAllociList[idx].mappingType;
480       schPdschCfg->timeDomRsrcAllociList[idx].startSymbol = \
481          macPdschCfg.timeDomRsrcAllociList[idx].startSymbol;
482       schPdschCfg->timeDomRsrcAllociList[idx].symbolLength = \
483          macPdschCfg.timeDomRsrcAllociList[idx].symbolLength;
484    }
485
486    schPdschCfg->rbgSize = macPdschCfg.rbgSize;
487    schPdschCfg->numCodeWordsSchByDci = macPdschCfg.numCodeWordsSchByDci;
488    schPdschCfg->bundlingType = macPdschCfg.bundlingType;
489    if(schPdschCfg->bundlingType == STATIC_BUNDLING_TYPE)
490    {
491       schPdschCfg->bundlingInfo.SchStaticBundling.size  = macPdschCfg.bundlingInfo.StaticBundling.size;
492    }
493    else if(schPdschCfg->bundlingType == DYNAMIC_BUNDLING_TYPE)
494    {
495       schPdschCfg->bundlingInfo.SchDynamicBundling.sizeSet1 = macPdschCfg.bundlingInfo.DynamicBundling.sizeSet1;
496       schPdschCfg->bundlingInfo.SchDynamicBundling.sizeSet2 = macPdschCfg.bundlingInfo.DynamicBundling.sizeSet2;
497    }
498    return ROK;
499 }
500
501 /*******************************************************************
502  *
503  * @brief Fill Initial DL BWP for SCH UE config
504  *
505  * @details
506  *
507  *    Function : fillInitialDlBwp
508  *
509  *    Functionality: Fill Initial DL BWP for SCH UE config
510  *
511  * @params[in] macInitDlBwp : Inital DL BWP cfg at MAC
512  *             schInitDlBwp : Inital DL BWP cfg to be filled
513  * @return ROK     - success
514  *         RFAILED - failure
515  *
516  * ****************************************************************/
517 uint8_t fillInitialDlBwp(InitialDlBwp macInitDlBwp, SchInitalDlBwp *schInitDlBwp)
518 {
519    schInitDlBwp->pdcchCfgPres = macInitDlBwp.pdcchPresent;
520    if(schInitDlBwp->pdcchCfgPres)
521    {
522       if(fillInitDlBwpPdcchCfg(macInitDlBwp.pdcchCfg, &schInitDlBwp->pdcchCfg) != ROK)
523       {
524          DU_LOG("\nMAC : fillInitDlBwpPdcchCfg() failed");
525          return RFAILED;
526       }
527    }
528
529    schInitDlBwp->pdschCfgPres = macInitDlBwp.pdschPresent;
530    if(schInitDlBwp->pdschCfgPres)
531    {
532       if(fillInitDlBwpPdschCfg(macInitDlBwp.pdschCfg,&schInitDlBwp->pdschCfg) != ROK)
533       {
534          DU_LOG("\nMAC : fillInitDlBwpPdschCfg() failed");
535          return RFAILED;
536       }
537    }
538    return ROK;
539 }
540
541 /*******************************************************************
542  *
543  * @brief Fills Sp Cell config to be sent to scheduler
544  *
545  * @details
546  *
547  *    Function : fillSpCellCfg 
548  *
549  *    Functionality: Fills Sp Cell config to be sent to scheduler
550  *
551  * @params[in] macSpCellCfg : SP cell cfg at MAC
552  *             schSpCellCfg : SP cell cfg to be filled
553  * @return ROK     - success
554  *         RFAILED - failure
555  *
556  * ****************************************************************/
557 uint8_t fillSpCellCfg(SpCellCfg macSpCellCfg, SchSpCellCfg *schSpCellCfg)
558 {
559    uint8_t   idx;
560    SchServCellCfgInfo   *servCellCfg;
561
562    schSpCellCfg->servCellIdx = macSpCellCfg.servCellIdx;
563    servCellCfg = &schSpCellCfg->servCellCfg;
564
565    /* Fill initial DL BWP */
566    if(fillInitialDlBwp(macSpCellCfg.servCellCfg.initDlBwp, \
567             &servCellCfg->initDlBwp) != ROK )
568    {
569       DU_LOG("\nMAC : fillInitialDlBwp() failed");
570       return RFAILED;
571    }
572
573    servCellCfg->numDlBwpToAdd = macSpCellCfg.servCellCfg.numDlBwpToAdd;
574    if(servCellCfg->numDlBwpToAdd > MAX_NUM_BWP)
575    {
576       DU_LOG("\nMAC : Number of DL BWP to ADD/MOD [%d] exceeds max limit [%d]",\
577             servCellCfg->numDlBwpToAdd, MAX_NUM_BWP);
578       return RFAILED;
579    }
580    for(idx = 0; idx < servCellCfg->numDlBwpToAdd; idx++)
581    {
582       /* TODO : As of now numDlBwpToAdd = 0 */
583    }
584
585    servCellCfg->firstActvDlBwpId =  macSpCellCfg.servCellCfg.firstActvDlBwpId;
586    servCellCfg->defaultDlBwpId = macSpCellCfg.servCellCfg.defaultDlBwpId;
587    servCellCfg->bwpInactivityTmr = NULL;
588    if(macSpCellCfg.servCellCfg.bwpInactivityTmr)
589    {
590       /* TODO : This is an optional parameter, not filled currently */
591    }
592
593    /* Fill PDSCH serving cell config */
594    if(fillPdschServCellCfg(macSpCellCfg.servCellCfg.pdschServCellCfg, \
595             &servCellCfg->pdschServCellCfg) != ROK)
596    {
597       DU_LOG("\nMAC : fillPdschServCellCfg() failed");
598       return RFAILED;
599    }
600
601    /* Fill Initail UL BWP */
602    if(fillInitialUlBwp(macSpCellCfg.servCellCfg.initUlBwp, \
603             &servCellCfg->initUlBwp) != ROK)
604    {
605       DU_LOG("\nMAC : fillInitialUlBwp() failed");
606       return RFAILED;
607    }
608
609    servCellCfg->numUlBwpToAdd = macSpCellCfg.servCellCfg.numUlBwpToAdd;
610    if(servCellCfg->numUlBwpToAdd > MAX_NUM_BWP)
611    {
612       DU_LOG("\nMAC : Number of UL BWP to ADD/MOD [%d] exceeds max limit [%d]",\
613             servCellCfg->numUlBwpToAdd, MAX_NUM_BWP);
614       return RFAILED;
615    }
616    for(idx = 0; idx < servCellCfg->numUlBwpToAdd; idx++)
617    {
618       /* TODO : As of now numDlBwpToAdd = 0 */
619    }
620    servCellCfg->firstActvUlBwpId =  macSpCellCfg.servCellCfg.firstActvUlBwpId;
621
622    return ROK;
623 }
624
625 /*******************************************************************
626  *
627  * @brief Sends UE configuration to Scheduler
628  *
629  * @details
630  *
631  *    Function : sendUeReqToSch
632  *
633  *    Functionality: sends UE configuration to Scheduler
634  *
635  * @params[in] Pst and Ue configuration
636  * @return ROK     - success
637  *         RFAILED - failure
638  *
639  * ****************************************************************/
640
641 uint8_t sendUeReqToSch(Pst *pst, SchUeCfg *schUeCfg)
642 {
643    Pst schPst;
644    switch(pst->event)
645    {
646       case EVENT_MAC_UE_CREATE_REQ:
647          FILL_PST_MAC_TO_SCH(schPst, EVENT_ADD_UE_CONFIG_REQ_TO_SCH);
648          return(*macSchAddUeConfigReqOpts[schPst.selector])(&schPst, schUeCfg);
649
650       case EVENT_MAC_UE_RECONFIG_REQ:
651          FILL_PST_MAC_TO_SCH(schPst, EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH);
652          return(*macSchModUeConfigReqOpts[schPst.selector])(&schPst,schUeCfg);
653       default: 
654          DU_LOG("\n Invalid Pst received %d", pst->event);
655          return RFAILED;
656    }
657 }
658
659 /*******************************************************************
660  *
661  * @brief Fills Sch Drb Qos Information
662  *
663  * @details
664  *
665  *    Function : fillSchDrbQosInfo
666  *
667  *    Functionality: Fills Sch Drb Qos Information
668  *
669  * @params[in] macLcCfg : Logical channel Cfg at MAC
670  *             schLcCfg : LC cfg to fill at scheduler
671  * @return ROK     - success
672  *         RFAILED - failure
673  *
674  * ****************************************************************/
675 void fillSchDrbQosInfo(DrbQosInfo *macDrbQos, SchDrbQosInfo *schDrbQos)
676 {
677    schDrbQos->fiveQiType  = macDrbQos->fiveQiType;
678    if(schDrbQos->fiveQiType == SCH_QOS_NON_DYNAMIC)
679    {
680       schDrbQos->u.nonDyn5Qi.fiveQi = macDrbQos->u.nonDyn5Qi.fiveQi;
681       schDrbQos->u.nonDyn5Qi.avgWindow = macDrbQos->u.nonDyn5Qi.avgWindow;
682       schDrbQos->u.nonDyn5Qi.maxDataBurstVol = macDrbQos->u.nonDyn5Qi.maxDataBurstVol;
683       schDrbQos->u.nonDyn5Qi.priorLevel =  macDrbQos->u.nonDyn5Qi.priorLevel;
684    }
685    else if(schDrbQos->fiveQiType == SCH_QOS_DYNAMIC)
686    {
687       schDrbQos->u.dyn5Qi.priorLevel         = macDrbQos->u.dyn5Qi.priorLevel;
688       schDrbQos->u.dyn5Qi.packetDelayBudget  = macDrbQos->u.dyn5Qi.packetDelayBudget;
689       schDrbQos->u.dyn5Qi.packetErrRateScalar= macDrbQos->u.dyn5Qi.packetErrRateScalar;
690       schDrbQos->u.dyn5Qi.packetErrRateExp   = macDrbQos->u.dyn5Qi.packetErrRateExp;
691       schDrbQos->u.dyn5Qi.fiveQi             = macDrbQos->u.dyn5Qi.fiveQi;
692       schDrbQos->u.dyn5Qi.delayCritical      = macDrbQos->u.dyn5Qi.delayCritical;
693       schDrbQos->u.dyn5Qi.avgWindow          = macDrbQos->u.dyn5Qi.avgWindow;
694       schDrbQos->u.dyn5Qi.maxDataBurstVol    = macDrbQos->u.dyn5Qi.maxDataBurstVol;
695    }
696    schDrbQos->ngRanRetPri.priorityLevel   = macDrbQos->ngRanRetPri.priorityLevel;
697    schDrbQos->ngRanRetPri.preEmptionCap   = macDrbQos->ngRanRetPri.preEmptionCap;
698    schDrbQos->ngRanRetPri.preEmptionVul   = macDrbQos->ngRanRetPri.preEmptionVul;
699    schDrbQos->grbQosFlowInfo.maxFlowBitRateDl = macDrbQos->grbQosInfo.maxFlowBitRateDl;
700    schDrbQos->grbQosFlowInfo.maxFlowBitRateUl = macDrbQos->grbQosInfo.maxFlowBitRateUl;
701    schDrbQos->grbQosFlowInfo.guarFlowBitRateDl= macDrbQos->grbQosInfo.guarFlowBitRateDl;
702    schDrbQos->grbQosFlowInfo.guarFlowBitRateUl= macDrbQos->grbQosInfo.guarFlowBitRateUl;
703    schDrbQos->pduSessionId = macDrbQos->pduSessionId;
704    schDrbQos->ulPduSessAggMaxBitRate = macDrbQos->ulPduSessAggMaxBitRate;
705 }
706
707 /*******************************************************************
708  *
709  * @brief Fill SCH UL logical channel configuration
710  *
711  * @details
712  *
713  *    Function : fillSchUlLcCfg
714  *
715  *    Functionality: Fills Sch Ul Lc configuration
716  *
717  * @params[in] macLcCfg : Logical channel Cfg at MAC
718  *             schLcCfg : LC cfg to fill at scheduler
719  * @return void
720  *
721  * ****************************************************************/
722
723 void fillSchUlLcCfg(SchUlLcCfg *schUlLcCfg, UlLcCfg *macUlLcCfg) 
724 {
725    schUlLcCfg->priority= macUlLcCfg->priority;
726    schUlLcCfg->lcGroup = macUlLcCfg->lcGroup;
727    schUlLcCfg->schReqId= macUlLcCfg->schReqId;
728    schUlLcCfg->pbr    = macUlLcCfg->pbr;    
729    schUlLcCfg->bsd    = macUlLcCfg->bsd;    
730 }
731
732 /*******************************************************************
733  *
734  * @brief Fill logical channel configuration
735  *
736  * @details
737  *
738  *    Function : fillLogicalChannelCfg
739  *
740  *    Functionality: Fill logical channel configuration
741  *
742  * @params[in] macLcCfg : Logical channel Cfg at MAC
743  *             schLcCfg : LC cfg to fill at scheduler
744  * @return ROK     - success
745  *         RFAILED - failure
746  *
747  * ****************************************************************/
748 uint8_t fillLogicalChannelCfg(SchLcCfg *schLcCfg, LcCfg *macLcCfg)
749 {
750    uint8_t sdIdx;
751    uint8_t ret = ROK;
752    schLcCfg->lcId = macLcCfg->lcId;
753    schLcCfg->configType = macLcCfg->configType;
754    schLcCfg->dlLcCfg.lcp = macLcCfg->dlLcCfg.lcp;
755    fillSchUlLcCfg(&schLcCfg->ulLcCfg, &macLcCfg->ulLcCfg);
756
757    if(macLcCfg->drbQos)
758    {
759      if(!schLcCfg->drbQos)
760      {
761         MAC_ALLOC(schLcCfg->drbQos, sizeof(SchDrbQosInfo));
762         if(!schLcCfg->drbQos)
763         {
764            DU_LOG("\nMAC : Memory alloc failed at drbQos at fillLogicalChannelCfg()");
765            ret = RFAILED;
766         }
767      }
768      if(ret == ROK)
769      {
770         fillSchDrbQosInfo(macLcCfg->drbQos, schLcCfg->drbQos);
771      }
772      else
773         return ret;
774    }
775    else
776    {
777       schLcCfg->drbQos = NULLP;
778    }
779
780    if(ret == ROK)
781    {
782       if(macLcCfg->snssai)
783       {
784          if(!schLcCfg->snssai)
785          {
786             MAC_ALLOC(schLcCfg->snssai, sizeof(SchSnssai));
787             if(!schLcCfg->snssai)
788             {
789                DU_LOG("\nMAC : Memory alloc failed at snssai at fillLogicalChannelCfg()");
790                ret = RFAILED;
791             }
792          }
793          if(ret == ROK)
794          {
795             schLcCfg->snssai->sst = macLcCfg->snssai->sst;
796             for(sdIdx = 0; sdIdx < SD_SIZE; sdIdx++)
797             {
798               schLcCfg->snssai->sd[sdIdx] = macLcCfg->snssai->sd[sdIdx];
799             }
800          }
801          else
802          {
803             schLcCfg->snssai = NULLP;
804             /*Freeing the previously allocated buffer in case of failure */
805             if(schLcCfg->drbQos)
806             {
807                MAC_FREE(schLcCfg->drbQos, sizeof(SchDrbQosInfo));
808                schLcCfg->drbQos = NULLP;
809             }
810             return ret;
811          }
812       }
813       else
814       {
815          schLcCfg->snssai = NULLP;
816       }
817    }
818
819    return ret;
820 }
821
822 /*******************************************************************
823  *
824  * @brief Fills Logical channel Cfg List to Add/Mod/Del
825  *
826  * @details
827  *
828  *    Function : fillSchLcCfgList
829  *
830  *    Functionality: Fills Logical channel Cfg List to Add/Mod/Del
831  *
832  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
833  * @return ROK     - success
834  *         RFAILED - failure
835  *
836  * ****************************************************************/
837
838 uint8_t fillSchLcCfgList(SchUeCfg *schUeCfg, MacUeCfg *ueCfg)
839 {
840    uint8_t lcIdx;
841
842    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
843    {
844       if(fillLogicalChannelCfg(&schUeCfg->schLcCfg[lcIdx], &ueCfg->lcCfgList[lcIdx]) != ROK)
845       {
846          DU_LOG("\nMAC : fillLogicalChannelCfg() failed for lc Idx[%d]", lcIdx);
847          return RFAILED;
848       }
849       schUeCfg->numLcs++;
850    }
851    return ROK;
852 }
853
854 /*******************************************************************
855  *
856  * @brief Fills and sends UE configuration to Scheduler
857  *
858  * @details
859  *
860  *    Function : fillSchUeCfg
861  *
862  *    Functionality: Fills and sends UE configuration to Scheduler
863  *
864  * @params[in] Ue configuration from DU APP
865  * @return ROK     - success
866  *         RFAILED - failure
867  *
868  * ****************************************************************/
869 uint8_t fillSchUeCfg(Pst *pst, SchUeCfg *schUeCfg, MacUeCfg *ueCfg)
870 {
871    uint8_t ret = ROK;
872
873    schUeCfg->cellId = ueCfg->cellId;
874    schUeCfg->crnti = ueCfg->crnti;
875
876    /* Copy MAC cell group config */
877    memset(&schUeCfg->macCellGrpCfg, 0, sizeof(SchMacCellGrpCfg));
878    if(fillMacCellGroupCfg(ueCfg->macCellGrpCfg, &schUeCfg->macCellGrpCfg) != ROK)
879    {
880       DU_LOG("\nMAC : fillMacCellGroupCfg() failed");
881       return RFAILED;
882    }
883
884    /* Copy Physical cell group config */
885    memset(&schUeCfg->phyCellGrpCfg, 0,sizeof(SchPhyCellGrpCfg));
886    if(fillPhyCellGroupCfg(ueCfg->phyCellGrpCfg, &schUeCfg->phyCellGrpCfg) != ROK)
887    {
888       DU_LOG("\nMAC : fillPhyCellGroupCfg() failed");
889       return RFAILED;
890    }
891
892    /* Copy sp cell config */
893    memset(&schUeCfg->spCellCfg, 0, sizeof(SchSpCellCfg));
894    if(fillSpCellCfg(ueCfg->spCellCfg, &schUeCfg->spCellCfg) != ROK)
895    {
896       DU_LOG("\nMAC : fillSpCellCfg() failed");
897       return RFAILED;
898    }
899
900    if(ueCfg->ambrCfg != NULLP)
901    {
902       MAC_ALLOC(schUeCfg->ambrCfg, sizeof(SchAmbrCfg));
903       if(!schUeCfg->ambrCfg)
904       {
905          DU_LOG("\nMAC : Memory allocation failed in sendReconfigReqToSch");
906          return RFAILED;
907       }
908       schUeCfg->ambrCfg->ulBr = ueCfg->ambrCfg->ulBr;
909       schUeCfg->ambrCfg->dlBr = ueCfg->ambrCfg->dlBr;
910    }
911    else
912       schUeCfg->ambrCfg = NULLP;
913
914    /* Fill DL modulation infor */
915    schUeCfg->dlModInfo.modOrder = ueCfg->dlModInfo.modOrder;
916    schUeCfg->dlModInfo.mcsIndex = ueCfg->dlModInfo.mcsIndex;
917    schUeCfg->dlModInfo.mcsTable = ueCfg->dlModInfo.mcsTable;
918
919    /* Fill UL modulation infor */
920    schUeCfg->ulModInfo.modOrder = ueCfg->ulModInfo.modOrder;
921    schUeCfg->ulModInfo.mcsIndex = ueCfg->ulModInfo.mcsIndex;
922    schUeCfg->ulModInfo.mcsTable = ueCfg->ulModInfo.mcsTable;
923
924    /* Fill sch Lc Cfg  to Add/ Mod/ Del */
925    ret  = fillSchLcCfgList(schUeCfg, ueCfg);
926    if(ret == RFAILED)
927    {
928       DU_LOG("\nMAC : Failed to copy LCs at fillSchUeCfg()");
929       return ret;
930    }
931    return ret;
932 }
933
934 /*******************************************************************
935  *
936  * @brief Update UeUlCb Lc List
937  *
938  * @details
939  *
940  *    Function : updateMacUlCb
941  *
942  *    Functionality: Update UeUlCb Lc List
943  *
944  * @params[in] delIdx, UeUlCb pointer
945  * @return void
946  *
947  * ****************************************************************/
948
949 void updateMacUlCb(uint8_t delIdx, UeUlCb *ulCb)
950 {
951    uint8_t lcIdx = 0;
952
953    for(lcIdx = delIdx; lcIdx < ulCb->numUlLc; lcIdx++)
954    {
955       /* Moving the array element to one step ahead */
956       memcpy(&ulCb->lcCb[lcIdx], &ulCb->lcCb[lcIdx+1], sizeof(UlLcCb));
957       memset(&ulCb->lcCb[lcIdx+1], 0, sizeof(UlLcCb));
958    }
959 }
960
961 /*******************************************************************
962  *
963  * @brief Update UeDlCb Lc List
964  *
965  * @details
966  *
967  *    Function : updateMacDlCb
968  *
969  *    Functionality: Update UeDlCb Lc List
970  *
971  * @params[in] delIdx, UeDlCb pointer
972  * @return void
973  *
974  * ****************************************************************/
975
976 void updateMacDlCb(uint8_t delIdx, UeDlCb *dlCb)
977 {
978    uint8_t lcIdx = 0;
979
980    for(lcIdx = delIdx; lcIdx < dlCb->numDlLc; lcIdx++)
981    {
982       /* Moving the array element to one step ahead */
983       memcpy(&dlCb->lcCb[lcIdx], &dlCb->lcCb[lcIdx+1], sizeof(DlLcCb));
984       memset(&dlCb->lcCb[lcIdx+1], 0, sizeof(DlLcCb));
985    }
986 }
987
988 /*******************************************************************
989  *
990  * @brief Fills Logical channel Cfg List to Add/Mod/Del
991  *
992  * @details
993  *
994  *    Function : fillMacLcCfgList
995  *
996  *    Functionality: Fills Logical channel Cfg List to Add/Mod/Del
997  *
998  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
999  * @return ROK     - success
1000  *         RFAILED - failure
1001  *
1002  * ****************************************************************/
1003
1004 uint8_t fillMacLcCfgList(MacUeCb *ueCb, MacUeCfg *ueCfg)
1005 {
1006    uint8_t lcIdx, ueLcIdx;
1007
1008    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
1009    {
1010       if(ueCb->dlInfo.numDlLc < MAX_NUM_LC)
1011       {
1012          if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
1013          {
1014             /*Filling DL LC CB */
1015             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1016             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcState = MAC_LC_STATE_ACTIVE;
1017             ueCb->dlInfo.numDlLc++;
1018             /*Filling UL LC CB */
1019             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1020             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1021             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcActive = MAC_LC_STATE_ACTIVE;
1022             ueCb->ulInfo.numUlLc++;
1023          }/*End of Add Config */
1024          else
1025          { 
1026             //searching for Lc to be Mod
1027             for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++)
1028             {
1029                if(ueCb->ulInfo.lcCb[ueLcIdx].lcId == ueCfg->lcCfgList[lcIdx].lcId)
1030                {
1031                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_MOD)
1032                   {
1033                      /*Nothing to Modify in DL LC CB */
1034                      /*Modify UL LC CB */
1035                      ueCb->ulInfo.lcCb[ueLcIdx].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1036                      DU_LOG("\nMAC: Successfully Modified LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1037                      break;
1038                   }
1039                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_DEL)
1040                   {
1041                      memset(&ueCb->dlInfo.lcCb[ueLcIdx], 0, sizeof(DlLcCb));
1042                      (ueCb->dlInfo.numDlLc)--;
1043                      updateMacDlCb(ueLcIdx, &ueCb->dlInfo);
1044
1045                      memset(&ueCb->ulInfo.lcCb[ueLcIdx], 0, sizeof(UlLcCb));
1046                      (ueCb->ulInfo.numUlLc)--;
1047                      updateMacUlCb(ueLcIdx, &ueCb->ulInfo);
1048                      DU_LOG("\nMAC: Successfully Deleted LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1049                      break;
1050                   }
1051                }
1052             }
1053          }/*End of Mod Config */
1054       }
1055    }
1056    return ROK;
1057 }
1058
1059 /*******************************************************************
1060  *
1061  * @brief Fills MAC UE Cb Cfg
1062  *
1063  * @details
1064  *
1065  *    Function : fillMacUeCb
1066  *
1067  *    Functionality: Fills MAC UE Cb Cfg
1068  *
1069  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
1070  *             cellIdx
1071  * @return ROK     - success
1072  *         RFAILED - failure
1073  *
1074  * ****************************************************************/
1075
1076 uint8_t fillMacUeCb(MacUeCb *ueCb, MacUeCfg *ueCfg, uint8_t cellIdx)
1077 {
1078    uint8_t ret = ROK;
1079
1080    ueCb->ueIdx = ueCfg->ueIdx;
1081    ueCb->crnti = ueCfg->crnti;
1082    ueCb->cellCb = macCb.macCell[cellIdx];
1083    ueCb->dlInfo.dlHarqEnt.numHarqProcs = \
1084       ueCfg->spCellCfg.servCellCfg.pdschServCellCfg.numHarqProcForPdsch; 
1085    ueCb->state = UE_STATE_ACTIVE;
1086    /*TODO: To check the bsr value during implementation */
1087    ueCb->bsrTmrCfg.periodicTimer = ueCfg->macCellGrpCfg.bsrTmrCfg.periodicTimer;
1088    ueCb->bsrTmrCfg.retxTimer     = ueCfg->macCellGrpCfg.bsrTmrCfg.retxTimer;
1089    ueCb->bsrTmrCfg.srDelayTimer  = ueCfg->macCellGrpCfg.bsrTmrCfg.srDelayTimer;
1090    ret = fillMacLcCfgList(ueCb, ueCfg);
1091    if(ret == RFAILED)
1092    {
1093       DU_LOG("\nMAC: Failed while filing MAC LC List at fillMacUeCb()");
1094    }
1095    return ret;
1096 }
1097
1098 /*******************************************************************
1099  *
1100  * @brief Function to update Mac Ra Cb
1101  *
1102  * @details
1103  *
1104  *    Function : updateMacRaCb
1105  *
1106  *    Functionality: Function to update Mac Ra Cb
1107  *
1108  * @params[in] cellIdx, Mac Ue Cb
1109  * @return ROK     - success
1110  *         RFAILED - failure
1111  *
1112  * ****************************************************************/
1113
1114 uint8_t updateMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1115 {
1116    uint8_t ueIdx;
1117    /* Copy RA Cb */
1118    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1119    {
1120       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1121       {
1122          ueCb->raCb = &macCb.macCell[cellIdx]->macRaCb[ueIdx];
1123          break;
1124       }
1125    }
1126    return ROK;
1127 }
1128
1129 /*******************************************************************
1130  *
1131  * @brief Function to delete Mac Ra Cb
1132  *
1133  * @details
1134  *
1135  *    Function : deleteMacRaCb
1136  *
1137  *    Functionality: Function to delete Mac Ra Cb
1138  *
1139  * @params[in] cellIdx, Mac Ue Cb
1140  * @return void
1141  *
1142  * ****************************************************************/
1143
1144 void deleteMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1145 {
1146    uint8_t ueIdx;
1147
1148    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1149    {
1150       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1151       {
1152          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
1153          {
1154            MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
1155                      macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
1156          }
1157          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu)
1158          {
1159             MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
1160                       macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize);
1161          }
1162          memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
1163          break;
1164       }
1165    }
1166                   
1167 }
1168
1169 /*******************************************************************
1170  *
1171  * @brief Creates UE Cb
1172  *
1173  * @details
1174  *
1175  *    Function : createUeCb
1176  *
1177  *    Functionality: Creates UE Cb
1178  *
1179  * @params[in] MAC UE Configuration
1180  * @return ROK     - success
1181  *         RFAILED - failure
1182  *
1183  * ****************************************************************/
1184 uint8_t createUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1185 {
1186    uint8_t ret =ROK;
1187
1188    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1189       &&(ueCb->state == UE_STATE_ACTIVE))
1190    {
1191       DU_LOG("\n MAC : CRNTI %d already configured ", ueCfg->crnti);
1192       return ROKDUP;
1193    }
1194    else
1195    {
1196       memset(ueCb, 0, sizeof(MacUeCb));
1197       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1198       if(ret != ROK)
1199       {
1200          DU_LOG("\nMAC : Failed to create Ue Cb at createUeCb()");
1201          return ret;
1202       }
1203       else
1204       {
1205          macCb.macCell[cellIdx]->numActvUe++;
1206          updateMacRaCb(cellIdx, ueCb);
1207          return ROK;
1208       }
1209
1210    }
1211
1212 }
1213
1214 /*******************************************************************
1215  *
1216  * @brief Modify UE Cb Cfg
1217  *
1218  * @details
1219  *
1220  *    Function : modifyUeCb
1221  *
1222  *    Functionality: modify UE Cb
1223  *
1224  * @params[in] MAC UE Configuration
1225  * @return ROK     - success
1226  *         RFAILED - failure
1227  *
1228  * ****************************************************************/
1229 uint8_t modifyUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1230 {
1231    uint8_t ret = ROK;
1232
1233    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1234       &&(ueCb->state == UE_STATE_ACTIVE))
1235    {
1236       DU_LOG("\n MAC : Modifying Ue config Req for CRNTI %d ", ueCfg->crnti);
1237       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1238       if(ret != ROK)
1239       {
1240          DU_LOG("\nMAC : Failed to modify MacUeCb at modifyUeCb()");
1241          return ret;
1242       }
1243       else
1244       {
1245          deleteMacRaCb(cellIdx, ueCb);
1246          return ROK;
1247       }
1248    }
1249    return RFAILED;
1250 }
1251
1252
1253 /*******************************************************************
1254  *
1255  * @brief Creates UE Cb and fills ueCfg
1256  *
1257  * @details
1258  *
1259  *    Function : procMacUeCfgData
1260  *
1261  *    Functionality: Creates UE Cb and fills ueCfg
1262  *
1263  * @params[in] MAC UE Configuration
1264  * @return ROK     - success
1265  *         RFAILED - failure
1266  *
1267  * ****************************************************************/
1268
1269 uint8_t procMacUeCfgData(Pst *pst, MacUeCfg *ueCfg)
1270 {
1271    uint8_t ret = ROK;
1272    uint16_t  cellIdx;
1273    MacUeCb   *ueCb = NULLP;
1274
1275
1276    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1277
1278    /* Validate cell id */
1279    if(macCb.macCell[cellIdx]->cellId != ueCfg->cellId)
1280    {
1281       DU_LOG("\nMAC : Cell Id %d not configured", ueCfg->cellId);
1282       return RFAILED;
1283    }
1284
1285    /* Check if max number of UE configured */
1286    if(macCb.macCell[cellIdx]->numActvUe > MAX_NUM_UE)
1287    {
1288       DU_LOG("MAC : Max number of UE [%d] already configured", MAX_NUM_UE);
1289       return RFAILED;
1290    }
1291
1292    /* Check if UE already configured */
1293    ueCb = &macCb.macCell[cellIdx]->ueCb[ueCfg->ueIdx -1];
1294    switch(pst->event)
1295    {
1296       case EVENT_UE_CONFIG_RSP_TO_MAC:
1297          ret = createUeCb(cellIdx, ueCb, ueCfg);
1298          if(ret != ROK)
1299             DU_LOG("\nMAC: AddUeConfigReq for cellIdx :%d failed in procMacUeCfgData()", cellIdx);
1300          break;
1301       case EVENT_UE_RECONFIG_RSP_TO_MAC:
1302          ret = modifyUeCb(cellIdx, ueCb, ueCfg);
1303          if(ret != ROK)
1304             DU_LOG("\nMAC: ModifyUeConfigReq for cellIdx :%d failed at procMacUeCfgData()", cellIdx);
1305          break;
1306       default:
1307          break;
1308    }
1309
1310    return ret;
1311 }
1312
1313 /*******************************************************************
1314  *
1315  * @brief Function to store the UeCfg Data 
1316  *
1317  * @details
1318  *
1319  *    Function : copyToTmpData
1320  *
1321  *    Functionality: Function to store the UeCfg Data
1322  *
1323  * @params[in] MacUeCfg pointer 
1324  * @return ROK     - success
1325  *         RFAILED - failure
1326  *
1327  * ****************************************************************/
1328
1329 uint8_t copyToTmpData(MacUeCfg *ueCfg)
1330 {
1331    uint8_t cellIdx;
1332    MacUeCfg *tmpData = NULLP;
1333
1334    MAC_ALLOC(tmpData, sizeof(MacUeCfg));
1335    if(!tmpData)
1336    {
1337       DU_LOG("\nMAC: Memory Alloc Failed at copyToTmpData()");
1338       return RFAILED;
1339    }
1340    memcpy(tmpData, ueCfg, sizeof(MacUeCfg));
1341    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1342    macCb.macCell[cellIdx]->ueCfgTmpData[ueCfg->ueIdx-1] = tmpData;
1343    return ROK;
1344 }
1345
1346 /*******************************************************************
1347  *
1348  * @brief Handles UE create requst from DU APP
1349  *
1350  * @details
1351  *
1352  *    Function : MacProcUeCreateReq
1353  *
1354  *    Functionality: Handles UE create requst from DU APP
1355  *
1356  * @params[in] 
1357  * @return ROK     - success
1358  *         RFAILED - failure
1359  *
1360  * ****************************************************************/
1361 uint8_t MacProcUeCreateReq(Pst *pst, MacUeCfg *ueCfg)
1362 {
1363    uint8_t ret = ROK;
1364    SchUeCfg   schUeCfg;
1365    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1366
1367    DU_LOG("\nMAC : UE Create Request for CRNTI[%d]", ueCfg->crnti);
1368
1369    if(ueCfg)
1370    {
1371       /*Storing received ueCfg in ueCfgTmpData */
1372       ret = copyToTmpData(ueCfg);
1373       if(ret == ROK)
1374       {
1375          /*Sending Cfg Req to SCH */
1376          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1377          if(ret != ROK)
1378             DU_LOG("\nMAC : Failed to fill Sch Ue Cfg at MacProcUeCreateReq()");
1379          else
1380          {
1381             /* Fill event and send UE create request to SCH */
1382             ret = sendUeReqToSch(pst, &schUeCfg);
1383             if(ret != ROK)
1384                DU_LOG("\nMAC : Failed to send UE Create request to SCH");
1385          }
1386       }
1387       else 
1388       {
1389          DU_LOG("\nMAC : Failed to store MAC UE CFG ");
1390       }
1391    }
1392    else
1393    {
1394       DU_LOG("\nMAC : MAC UE Create request processing failed");
1395       ret = RFAILED;
1396    }
1397    /* FREE shared memory */
1398    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1399
1400    return ret;
1401 }
1402
1403 /*******************************************************************
1404  *
1405  * @brief Fill and Send UE create response from MAC to DU APP
1406  *
1407  * @details
1408  *
1409  *    Function : MacSendUeCreateRsp
1410  *
1411  *    Functionality: Fill and Send UE create response from MAC to DUAPP
1412  *
1413  * @params[in] MAC UE create result
1414  *             SCH UE create response
1415  * @return ROK     - success
1416  *         RFAILED - failure
1417  *
1418  * ****************************************************************/
1419 uint8_t MacSendUeCreateRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1420 {
1421    MacUeCfgRsp   *cfgRsp;
1422    Pst        rspPst;
1423
1424    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1425    if(!cfgRsp)
1426    {
1427       DU_LOG("\nMAC: Memory allocation for UE config response failed");
1428       return RFAILED;
1429    }
1430
1431    /* Filling UE Config response */
1432    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1433    cfgRsp->cellId = schCfgRsp->cellId;
1434    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1435    cfgRsp->result = result;
1436
1437    /* Fill Post structure and send UE Create response*/
1438    memset(&rspPst, 0, sizeof(Pst));
1439    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_CREATE_RSP);
1440    return (*macDuUeCfgRspOpts[rspPst.selector])(&rspPst, cfgRsp); 
1441 }
1442
1443 /*******************************************************************
1444  *
1445  * @brief Fill and Send UE Reconfig response from MAC to DU APP
1446  *
1447  * @details
1448  *
1449  *    Function : MacSendUeReconfigRsp
1450  *
1451  *    Functionality: Fill and Send UE Reconfig response from MAC to DUAPP
1452  *
1453  * @params[in] MAC UE create result
1454  *             SCH UE create response
1455  * @return ROK     - success
1456  *         RFAILED - failure
1457  *
1458  * ****************************************************************/
1459 uint8_t MacSendUeReconfigRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1460 {
1461    MacUeCfgRsp   *cfgRsp;
1462    Pst        rspPst;
1463
1464    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1465    if(!cfgRsp)
1466    {
1467       DU_LOG("\nMAC: Memory allocation for UE Reconfig response failed");
1468       return RFAILED;
1469    }
1470
1471    /* Filling UE Config response */
1472    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1473    cfgRsp->cellId = schCfgRsp->cellId;
1474    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1475    cfgRsp->result = result;
1476
1477    /* Fill Post structure and send UE Create response*/
1478    memset(&rspPst, 0, sizeof(Pst));
1479    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_RECONFIG_RSP);
1480    return (*macDuUeCfgRspOpts[rspPst.selector])(&rspPst, cfgRsp);
1481 }
1482
1483 /*******************************************************************
1484  *
1485  * @brief  Function to return Mac Ue Cfg pointer
1486  *
1487  * @details
1488  *
1489  *    Function : getMacUeCfg
1490  *
1491  *    Functionality:
1492  *      Function to return Mac Ue Cfg pointer
1493  *
1494  * @params[in] cellIdx, ueIdx
1495  *
1496  * @return MacUeCfg pointer - success
1497  *         NULLP - failure
1498  *
1499  * ****************************************************************/
1500
1501 MacUeCfg *getMacUeCfg(uint16_t cellIdx, uint8_t ueIdx)
1502 {
1503    MacUeCfg *ueCfg = NULLP;
1504    if(macCb.macCell[cellIdx])
1505    {
1506       ueCfg = macCb.macCell[cellIdx]->ueCfgTmpData[ueIdx-1];
1507    }
1508    else
1509    {
1510       DU_LOG("\nMAC: Failed to get macCellCb in getMacUeCfg()");
1511    }
1512    return ueCfg;
1513 }
1514
1515 /*******************************************************************
1516  *
1517  * @brief  Processes UE create response from scheduler
1518  *
1519  * @details
1520  *
1521  *    Function : MacProcSchUeCfgRsp
1522  *
1523  *    Functionality:
1524  *      Processes UE create response from scheduler
1525  *      Sends UE create response to DU APP
1526  *
1527  * @params[in] Pst : Post structure
1528  *             schCfgRsp : Scheduler UE cfg response
1529  * @return ROK     - success
1530  *         RFAILED - failure
1531  *
1532  * ****************************************************************/
1533 uint8_t MacProcSchUeCfgRsp(Pst *pst, SchUeCfgRsp *schCfgRsp)
1534 {
1535    uint8_t result = MAC_DU_APP_RSP_NOK;
1536    uint8_t ret = ROK;
1537    uint16_t cellIdx;
1538    MacUeCfg *ueCfg = NULLP;
1539
1540    GET_CELL_IDX(schCfgRsp->cellId, cellIdx);
1541    ueCfg = getMacUeCfg(cellIdx, schCfgRsp->ueIdx);
1542    if(ueCfg == NULLP)
1543    {
1544       DU_LOG("\nMAC : Failed to find the Mac Ue Cfg for event [%d] in MacProcSchUeCfgRsp()", pst->event);
1545       ret = RFAILED;
1546    }
1547
1548    switch(pst->event)
1549    {
1550       case EVENT_UE_CONFIG_RSP_TO_MAC:
1551       {
1552          if(schCfgRsp->rsp != RSP_NOK)
1553          {
1554             DU_LOG("\nMAC: SCH UeConfigRsp for CRNTI[%d] is success in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1555             if(ret == ROK)
1556             {
1557                ret = procMacUeCfgData(pst, ueCfg);
1558                if(ret == ROK)
1559                {
1560                   result = MAC_DU_APP_RSP_OK;
1561                }
1562             }
1563          }
1564          else
1565          {
1566             DU_LOG("\nMAC: SCH UeConfigRsp for CRNTI[%d] is failed in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1567          }
1568          ret = MacSendUeCreateRsp(result, schCfgRsp);
1569       }
1570       break;
1571
1572       case EVENT_UE_RECONFIG_RSP_TO_MAC:
1573       {
1574          if(schCfgRsp->rsp != RSP_NOK)
1575          {
1576             DU_LOG("\nMAC: SCH UeReconfigRsp for CRNTI[%d] is success in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1577             if(ret == ROK)
1578             {
1579                ret = procMacUeCfgData(pst, ueCfg);
1580                if(ret == ROK)
1581                {
1582                   result = MAC_DU_APP_RSP_OK;
1583                }
1584             }
1585          }
1586          else
1587          {
1588             DU_LOG("\nMAC: SCH UeReconfigRsp for CRNTI[%d] is failed in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1589          }
1590          ret = MacSendUeReconfigRsp(result, schCfgRsp);
1591       }
1592       break;
1593       
1594       default:
1595       break;
1596    }
1597    MAC_FREE(ueCfg, sizeof(MacUeCfg));
1598    ueCfg = NULLP;
1599    
1600    return ret; 
1601 }
1602
1603 /*******************************************************************
1604  *
1605  * @brief Handles UE Reconfig requst from DU APP
1606  *
1607  * @details
1608  *
1609  *    Function : MacProcUeReconfigReq
1610  *
1611  *    Functionality: Handles UE Reconfig requst from DU APP
1612  *
1613  * @params[in] 
1614  * @return ROK     - success
1615  *         RFAILED - failure
1616  *
1617  * ****************************************************************/
1618 uint8_t MacProcUeReconfigReq(Pst *pst, MacUeCfg *ueCfg)
1619 {
1620    uint8_t ret = ROK;
1621    SchUeCfg   schUeCfg;
1622    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1623
1624    DU_LOG("\nMAC : UE Reconfig Request for CRNTI[%d]", ueCfg->crnti);
1625
1626    if(ueCfg)
1627    {
1628       /*Storing received ueCfg in ueCfgTmpData */
1629       ret = copyToTmpData(ueCfg);
1630       if(ret == ROK)
1631       {
1632          /*Sending Cfg Req to SCH */
1633          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1634          if(ret != ROK)
1635             DU_LOG("\nMAC : Failed to fill sch Ue Cfg at MacProcUeReconfigReq()");
1636          else
1637          {
1638             /* Fill event and send UE create request to SCH */
1639             ret = sendUeReqToSch(pst, &schUeCfg);
1640             if(ret != ROK)
1641                DU_LOG("\nMAC : Failed to send UE Reconfig Request to SCH");
1642          }
1643       }
1644       else 
1645       {
1646          DU_LOG("\nMAC : Failed to store MAC UE Cb ");
1647       }
1648    }
1649    else
1650    {
1651       DU_LOG("\nMAC : MAC UE Create request processing failed");
1652       ret = RFAILED;
1653    }
1654    /* FREE shared memory */
1655    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1656    return ROK;
1657 }
1658
1659
1660 /**********************************************************************
1661   End of file
1662  **********************************************************************/