Memory handling fixes
[o-du/l2.git] / src / 5gnrsch / sch_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 /* This file contains UE management handling functionality for SCH */
19
20 #include "common_def.h"
21 #include "tfu.h"
22 #include "lrg.h"
23
24 #include "tfu.x"
25 #include "lrg.x"
26 #include "du_log.h"
27 #include "du_app_mac_inf.h"
28 #include "mac_sch_interface.h"
29 #include "sch.h"
30 #include "sch_utils.h"
31
32 /* local defines */
33 SchUeCfgRspFunc SchUeCfgRspOpts[] =
34 {
35    packSchUeCfgRsp,      /* LC */
36    MacProcSchUeCfgRsp,   /* TC */
37    packSchUeCfgRsp       /* LWLC */
38 };
39
40 SchUeDeleteRspFunc SchUeDeleteRspOpts[] =
41 {
42    packSchUeDeleteRsp,      /* LC */
43    MacProcSchUeDeleteRsp,   /* TC */
44    packSchUeDeleteRsp       /* LWLC */
45 };
46
47 SchCellDeleteRspFunc SchCellDeleteRspOpts[]=
48 {
49    packSchCellDeleteRsp,      /* LC */
50    MacProcSchCellDeleteRsp,   /* TC */
51    packSchCellDeleteRsp       /* LWLC */
52 };
53 /*******************************************************************
54  *
55  * @brief Fill and send UE cfg response to MAC
56  *
57  * @details
58  *
59  *    Function : SchSendUeCfgRspToMac
60  *
61  *    Functionality: Fill and send UE cfg response to MAC
62  *
63  * @params[in] 
64  * @return ROK     - success
65  *         RFAILED - failure
66  *
67  * ****************************************************************/
68 void SchSendUeCfgRspToMac(uint16_t event, SchUeCfg *ueCfg, Inst inst,\
69       SchMacRsp result, SchUeCfgRsp *cfgRsp)
70 {
71    Pst rspPst;
72
73    cfgRsp->cellId = ueCfg->cellId;
74    cfgRsp->crnti = ueCfg->crnti;
75    GET_UE_IDX(ueCfg->crnti, cfgRsp->ueIdx);
76    cfgRsp->rsp = result;   
77
78    /* Filling response post */
79    memset(&rspPst, 0, sizeof(Pst));
80    FILL_PST_SCH_TO_MAC(rspPst, inst);
81    if(event == EVENT_ADD_UE_CONFIG_REQ_TO_SCH)
82    {
83       rspPst.event = EVENT_UE_CONFIG_RSP_TO_MAC;
84       DU_LOG("\nINFO  -->  SCH :  Sending UE Config response to MAC");
85    }
86    else if(event == EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH)
87    {
88       rspPst.event = EVENT_UE_RECONFIG_RSP_TO_MAC;
89       DU_LOG("\nINFO  -->  SCH :  Sending UE Reconfig response to MAC");
90    }
91    SchUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp);
92 }
93
94 /*******************************************************************
95  
96  *
97  * @brief Function to fill Dl Lc Context in SCH Ue Cb
98  *
99  * @details
100  *
101  *    Function : fillSchDlLcCtxt
102  *
103  *    Functionality: Function to fill Dl Lc Context in SCH Ue Cb
104  *
105  * @params[in] SchDlLcCtxt pointer,
106  *             SchLcCfg pointer
107  * @return void
108  *
109  * ****************************************************************/
110
111 void fillSchDlLcCtxt(SchDlLcCtxt *ueCbLcCfg, SchLcCfg *lcCfg)
112 {
113    ueCbLcCfg->lcId = lcCfg->lcId;
114    ueCbLcCfg->lcp = lcCfg->dlLcCfg.lcp;
115    ueCbLcCfg->lcState = SCH_LC_STATE_ACTIVE;
116    ueCbLcCfg->bo = 0;
117 }
118
119 /*******************************************************************
120  *
121  * @brief Function to fill Ul Lc Context in SCH Ue Cb
122  *
123  * @details
124  *
125  *    Function : fillSchUlLcCtxt
126  *
127  *    Functionality: Function to fill Ul Lc Context in SCH Ue Cb
128  *
129  * @params[in] SchUlLcCtxt pointer,
130  *             SchLcCfg pointer
131  * @return void
132  *
133  * ****************************************************************/
134
135 void fillSchUlLcCtxt(SchUlLcCtxt *ueCbLcCfg, SchLcCfg *lcCfg)
136 {
137    ueCbLcCfg->lcId = lcCfg->lcId;
138    ueCbLcCfg->lcState = SCH_LC_STATE_ACTIVE;
139    ueCbLcCfg->priority = lcCfg->ulLcCfg.priority;
140    ueCbLcCfg->lcGroup = lcCfg->ulLcCfg.lcGroup;
141    ueCbLcCfg->schReqId = lcCfg->ulLcCfg.schReqId;
142    ueCbLcCfg->pbr     = lcCfg->ulLcCfg.pbr;
143    ueCbLcCfg->bsd     = lcCfg->ulLcCfg.bsd;
144
145 }
146
147 /*******************************************************************
148  *
149  * @brief Function to update Sch Ul Lc Cb
150  *
151  * @details
152  *
153  *    Function : updateSchUlCb
154  *
155  *    Functionality: Function to update SCH Ul Lc Cb
156  *
157  * @returns void
158  *
159  * ****************************************************************/
160
161 void updateSchUlCb(uint8_t delIdx, SchUlCb *ulInfo)
162 {
163    uint8_t lcIdx = 0;
164
165    for(lcIdx = delIdx; lcIdx < ulInfo->numUlLc; lcIdx++)
166    {
167       memcpy(&ulInfo->ulLcCtxt[lcIdx], &ulInfo->ulLcCtxt[lcIdx+1], sizeof(SchUlLcCtxt));
168       memset(&ulInfo->ulLcCtxt[lcIdx+1], 0, sizeof(SchUlLcCtxt));
169    }
170 }
171
172 /*******************************************************************
173  *
174  * @brief Function to update SCH Dl Lc Cb
175  *
176  * @details
177  *
178  *    Function : updateSchDlCb
179  *
180  *    Functionality: Function to update SCH DL Lc Cb
181  *
182  * @returns void
183  *
184  * ****************************************************************/
185
186 void updateSchDlCb(uint8_t delIdx, SchDlCb *dlInfo)
187 {
188    uint8_t lcIdx = 0;
189
190    for(lcIdx = delIdx; lcIdx < dlInfo->numDlLc; lcIdx++)
191    {
192       memcpy(&dlInfo->dlLcCtxt[lcIdx], &dlInfo->dlLcCtxt[lcIdx+1], sizeof(SchDlLcCtxt));
193       memset(&dlInfo->dlLcCtxt[lcIdx+1], 0, sizeof(SchDlLcCtxt));
194    }
195 }
196
197 /*******************************************************************
198  *
199  * @brief Function to fill SchUeCb
200  *
201  * @details
202  *
203  *    Function : fillSchUeCb
204  *
205  *    Functionality: Function to fill SchUeCb
206  *
207  * @params[in] SchUeCb pointer,
208  *             SchUeCfg pointer
209  * @return ROK/RFAILED
210  *
211  * ****************************************************************/
212
213 uint8_t fillSchUeCb(SchUeCb *ueCb, SchUeCfg *ueCfg)
214 {
215    uint8_t   lcIdx, ueLcIdx;
216   
217    ueCb->ueCfg.cellId = ueCfg->cellId;
218    ueCb->ueCfg.crnti = ueCfg->crnti;
219    if(ueCfg->macCellGrpCfgPres == true)
220    {
221       memcpy(&ueCb->ueCfg.macCellGrpCfg , &ueCfg->macCellGrpCfg, sizeof(SchMacCellGrpCfg)); 
222       ueCb->ueCfg.macCellGrpCfgPres = true;
223    }
224
225    if(ueCfg->phyCellGrpCfgPres == true)
226    {
227       memcpy(&ueCb->ueCfg.phyCellGrpCfg ,  &ueCfg->phyCellGrpCfg, sizeof(SchPhyCellGrpCfg));
228       ueCb->ueCfg.phyCellGrpCfgPres = true;
229    }
230
231    if(ueCfg->spCellCfgPres == true)
232    {
233       memcpy(&ueCb->ueCfg.spCellCfg , &ueCfg->spCellCfg, sizeof(SchSpCellCfg));
234       ueCb->ueCfg.spCellCfgPres = true;
235    }
236
237    ueCb->state = SCH_UE_STATE_ACTIVE;
238    if(ueCfg->ambrCfg)
239    {
240       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
241       ueCb->ueCfg.ambrCfg =  ueCfg->ambrCfg;
242    }
243    memcpy(&ueCb->ueCfg.dlModInfo,  &ueCfg->dlModInfo , sizeof(SchModulationInfo));
244    memcpy(&ueCb->ueCfg.ulModInfo,  &ueCfg->ulModInfo , sizeof(SchModulationInfo));
245
246    //Updating SchUlCb and SchDlCb DB in SchUeCb
247    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
248    {
249       if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_ADD)
250       {
251          fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueCb->ulInfo.numUlLc], &ueCfg->schLcCfg[lcIdx]);
252          ueCb->ulInfo.numUlLc++;
253          fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueCb->dlInfo.numDlLc], &ueCfg->schLcCfg[lcIdx]);
254          ueCb->dlInfo.numDlLc++;
255       }
256       else
257       {
258          for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++) //searching for Lc to be Mod
259          {
260             if(ueCb->ulInfo.ulLcCtxt[ueLcIdx].lcId == ueCfg->schLcCfg[lcIdx].lcId)
261             {
262                if(ueCfg->schLcCfg[lcIdx].configType == CONFIG_MOD)
263                {
264                   fillSchUlLcCtxt(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
265                   fillSchDlLcCtxt(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], &ueCfg->schLcCfg[lcIdx]);
266                   break;
267                }
268                if(ueCfg->schLcCfg[ueLcIdx].configType == CONFIG_DEL)
269                {
270                   memset(&ueCb->ulInfo.ulLcCtxt[ueLcIdx], 0, sizeof(SchUlLcCtxt));
271                   ueCb->ulInfo.numUlLc--;
272                   updateSchUlCb(ueLcIdx, &ueCb->ulInfo); //moving arr elements one idx ahead 
273                   memset(&ueCb->dlInfo.dlLcCtxt[ueLcIdx], 0, sizeof(SchDlLcCtxt));
274                   ueCb->dlInfo.numDlLc--;
275                   updateSchDlCb(ueLcIdx, &ueCb->dlInfo); //moving arr elements one idx ahead
276                   break;
277                }
278             }
279          }/*End of inner for loop */
280       }
281
282       SCH_FREE(ueCfg->schLcCfg[lcIdx].drbQos, sizeof(SchDrbQosInfo));
283       SCH_FREE(ueCfg->schLcCfg[lcIdx].snssai, sizeof(SchSnssai));
284
285    }/* End of outer for loop */
286    return ROK;
287 }
288
289 /*******************************************************************
290  *
291  * @brief Function to get SCH Cell Cb
292  *
293  * @details
294  *
295  *    Function : getSchCellCb
296  *
297  *    Functionality: Function to get SCH Cell Cb
298  *
299  * @params[in] event, SchUeCfg pointer 
300  * @return schUeCb pointer  - success
301  *         NULLP - failure
302  *
303  * ****************************************************************/
304
305 SchCellCb *getSchCellCb(uint16_t srcEvent, Inst inst, SchUeCfg *ueCfg)
306 {
307    uint8_t      idx;
308    SchCellCb    *cellCb = NULLP;
309    SchUeCfgRsp  cfgRsp;
310    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
311
312    /* Search of cell cb */
313    for(idx = 0; idx < MAX_NUM_CELL; idx++)
314    {
315       cellCb = schCb[inst].cells[idx];
316       if(cellCb->cellId == ueCfg->cellId)
317          break;
318    }
319    if(idx == MAX_NUM_CELL)
320    {
321       DU_LOG("\nERROR  -->  SCH : Ue create request failed. Invalid cell id %d", ueCfg->cellId);
322       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
323       return NULLP;
324    }
325
326    /* Check if max number of UE configured */
327    if(cellCb->numActvUe > MAX_NUM_UE)
328    {
329       DU_LOG("\nERROR  -->  SCH :  Max number of UE [%d] already configured", MAX_NUM_UE);
330       SchSendUeCfgRspToMac(srcEvent, ueCfg, inst, RSP_NOK, &cfgRsp);
331       return NULLP;
332    }
333    return cellCb;
334 }
335
336
337 /*******************************************************************
338  *
339  * @brief Function to Add Ue Config Request from MAC
340  *
341  * @details
342  *
343  *    Function : MacSchAddUeConfigReq
344  *
345  *    Functionality: Function to Add Ue config request from MAC
346  *
347  * @params[in] 
348  * @return ROK     - success
349  *         RFAILED - failure
350  *
351  * ****************************************************************/
352 uint8_t MacSchAddUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
353 {
354    uint8_t ueIdx, lcIdx, ret = ROK;
355    SchCellCb    *cellCb = NULLP;
356    SchUeCb      *ueCb = NULLP;
357    SchUeCfgRsp  cfgRsp;
358    Inst         inst = pst->dstInst - 1;
359    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
360
361    if(!ueCfg)
362    {
363       DU_LOG("\nERROR  -->  SCH :  Adding UE Config Request failed at MacSchAddUeConfigReq()");
364       return RFAILED;
365    }
366    DU_LOG("\nDEBUG  -->  SCH :  Adding UE Config Request for CRNTI[%d]", ueCfg->crnti);
367    cellCb = getSchCellCb(pst->event, inst, ueCfg);
368
369    /* Search if UE already configured */
370    GET_UE_IDX(ueCfg->crnti, ueIdx);
371    ueCb = &cellCb->ueCb[ueIdx -1];
372    if(ueCb)
373    {
374       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
375       {
376          DU_LOG("\nDEBUG  -->  SCH : CRNTI %d already configured ", ueCfg->crnti);
377          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
378          return ROK;
379       }
380    }
381    else
382    {
383       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchAddUeConfigReq() ");
384       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
385       return RFAILED;
386    }
387
388    /* Fill received Ue Configuration in UeCb */
389    memset(ueCb, 0, sizeof(SchUeCb));
390    GET_UE_IDX(ueCfg->crnti, ueIdx);
391    ueCb->ueIdx = ueIdx;
392    ueCb->crnti = ueCfg->crnti;
393    ueCb->state = SCH_UE_STATE_ACTIVE;
394    ret = fillSchUeCb(ueCb, ueCfg);
395    if(ret == ROK)
396    {
397       cellCb->numActvUe++;
398       SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
399       ueCb->cellCb = cellCb;
400       ueCb->srRcvd = false;
401       for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
402          ueCb->bsrInfo[lcIdx].dataVol = 0;
403
404       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
405    }
406    return ret;
407 }
408
409 /*******************************************************************
410 *
411 * @brief Fills PUSCH UL allocation
412 *
413 * @details
414 *
415 *    Function : schFillPuschAlloc
416 *
417 *    Functionality: fills PUSCH info
418 *
419 * @params[in]
420 * @return ROK     - success
421 *         RFAILED - failure
422 *
423 * ****************************************************************/
424 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
425 {
426   uint16_t puschSlot      = 0;
427   uint16_t startRb        = 0;
428   uint8_t  numRb          = 0;
429   uint16_t tbSize         = 0;
430   uint8_t  buffer         = 5;
431   uint8_t  idx            = 0;
432   SchCellCb *cellCb       = ueCb->cellCb;
433   SchUlSlotInfo *schUlSlotInfo = NULLP;
434   uint8_t k2=0, startSymb=0 , symbLen=11;
435   
436   if(ueCb->ueCfg.spCellCfgPres == true)
437   {
438      k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
439      startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
440      symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
441   }
442   puschSlot = (pdcchSlot + k2) % cellCb->numSlots;
443
444   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
445   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
446   numRb   = schCalcNumPrb(tbSize, ueCb->ueCfg.ulModInfo.mcsIndex, symbLen);
447   /* increment PUSCH PRB */
448
449   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
450
451   puschInfo->crnti             = ueCb->crnti; 
452   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
453   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
454   puschInfo->fdAlloc.startPrb  = startRb;
455   puschInfo->fdAlloc.numPrb    = numRb;
456   puschInfo->tdAlloc.startSymb = startSymb;
457   puschInfo->tdAlloc.numSymb   = symbLen;
458   puschInfo->tbInfo.qamOrder   = ueCb->ueCfg.ulModInfo.modOrder;
459   puschInfo->tbInfo.mcs        = ueCb->ueCfg.ulModInfo.mcsIndex;
460   puschInfo->tbInfo.mcsTable   = ueCb->ueCfg.ulModInfo.mcsTable;
461   puschInfo->tbInfo.ndi        = 1; /* new transmission */
462   puschInfo->tbInfo.rv         = 0;
463   puschInfo->tbInfo.tbSize     = tbSize;
464   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
465   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
466   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
467
468   /* Update pusch in cell */
469   for(idx=startSymb; idx<symbLen; idx++)
470   {
471      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
472   }
473
474   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
475
476   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
477   if(!schUlSlotInfo->schPuschInfo)
478   {
479      DU_LOG("\nERROR  -->  SCH: Memory allocation failed in schAllocMsg3Pusch");
480      return RFAILED;
481   }
482   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
483
484   return ROK;
485 }
486
487 /*******************************************************************
488  *
489  * @brief Fills DCI for UL grant
490  *
491  * @details
492  *
493  *    Function : schFillUlDci
494  *
495  *    Functionality: fills DCI for UL grant in response to BSR
496  *
497  * @params[in]
498  * @return ROK     - success
499  *         RFAILED - failure
500  *
501  * ****************************************************************/
502 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
503 {
504    SchCellCb         *cellCb  = ueCb->cellCb;
505    SchControlRsrcSet coreset1 ;
506   
507    memset(&coreset1, 0, sizeof(SchControlRsrcSet));
508    if(ueCb->ueCfg.spCellCfgPres == true)
509    {
510      coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
511    }
512    
513    dciInfo->cellId = cellCb->cellId;
514    dciInfo->crnti  = ueCb->crnti;
515
516    /* fill bwp cfg */
517    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
518    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
519    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
520    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
521
522    /*fill coreset cfg */
523    //Considering number of RBs in coreset1 is same as coreset0
524    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
525    //Considering coreset1 also starts from same symbol as coreset0
526    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
527    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
528    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
529    
530    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
531    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
532    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
533    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
534    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
535    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
536    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
537    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
538    
539    dciInfo->formatType = FORMAT0_0;
540    
541    /* fill UL grant */
542    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
543    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
544    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
545    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
546    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
547    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
548    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
549    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
550    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
551    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
552    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
553    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
554    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
555    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
556    
557    /* Fill DCI Structure */
558    dciInfo->dciInfo.rnti                              = ueCb->crnti;
559    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
560    dciInfo->dciInfo.scramblingRnti                    = 0;
561    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
562    dciInfo->dciInfo.aggregLevel                       = 4;
563    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
564    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
565    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
566    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
567    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
568    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
569    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
570    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
571
572    return ROK;
573 }
574
575 /*******************************************************************
576  *
577  * @brief Function to Modify Ue Config request from MAC
578  *
579  * @details
580  *
581  *    Function : MacSchModUeConfigReq
582  *
583  *    Functionality: Function to modify Ue Config request from MAC
584  *
585  * @params[in] 
586  * @return ROK     - success
587  *         RFAILED - failure
588  *
589  * ****************************************************************/
590 uint8_t MacSchModUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
591 {
592    uint8_t ueIdx, lcIdx, ret = ROK;
593    SchCellCb    *cellCb = NULLP;
594    SchUeCb      *ueCb = NULLP;
595    SchUeCfgRsp  cfgRsp;
596    Inst         inst = pst->dstInst - 1;
597    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
598
599    if(!ueCfg)
600    {
601       DU_LOG("\nERROR  -->  SCH : Modifying Ue Config request failed at MacSchModUeConfigReq()");
602       return RFAILED;
603    }
604    DU_LOG("\nDEBUG  -->  SCH : Modifying Ue Config Request for CRNTI[%d]", ueCfg->crnti);
605    cellCb = getSchCellCb(pst->event, inst, ueCfg);
606
607    /* Search if UE already configured */
608    GET_UE_IDX(ueCfg->crnti, ueIdx);
609    ueCb = &cellCb->ueCb[ueIdx -1];
610    
611    if(!ueCb)
612    {
613       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchModUeConfigReq() ");
614       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
615       return RFAILED;
616    }
617    if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
618    {
619       /* Found the UeCb to Reconfig */
620       ret = fillSchUeCb(ueCb, ueCfg);
621       if(ret == ROK)
622       {
623          ueCb->cellCb = cellCb;
624          ueCb->srRcvd = false;
625          for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
626             ueCb->bsrInfo[lcIdx].dataVol = 0;
627
628          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
629       }
630    }
631    return ret;
632 }
633
634 /*******************************************************************
635 *
636 * @brief Fill and send UE delete response to MAC
637 *
638 * @details
639 *
640 *    Function :  SchSendUeDeleteRspToMac
641 *
642 *    Functionality: Fill and send UE delete response to MAC
643 *
644 * @params[in] Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, 
645 *              ErrorCause cause
646 * @return ROK     - success
647 *         RFAILED - failure
648 *
649 * ****************************************************************/
650 void SchSendUeDeleteRspToMac(Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, ErrorCause cause)
651 {
652     Pst rspPst;
653     SchUeDeleteRsp  delRsp;
654     
655     memset(&delRsp, 0, sizeof(SchUeDeleteRsp));
656     delRsp.cellId = ueDelete->cellId;
657     delRsp.crnti = ueDelete->crnti;
658     delRsp.rsp = result; 
659     delRsp.cause = cause; 
660
661     /* Filling response post */
662     memset(&rspPst, 0, sizeof(Pst));
663     FILL_PST_SCH_TO_MAC(rspPst, inst);
664     rspPst.event = EVENT_UE_DELETE_RSP_TO_MAC;
665     SchUeDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
666 }
667
668 /*******************************************************************
669 *
670 * @brief Function to delete Sch Pucch ResrcCfg
671 *
672 * @details
673 *
674 *    Function : deleteSchPucchResrcCfg 
675 *
676 *    Functionality: Function to delete Sch Pucch ResrcCfg
677 *
678 * @params[in] SchPucchResrcCfg *resrc
679 * @return void 
680 *
681 * ****************************************************************/
682
683 void deleteSchPucchResrcCfg(SchPucchResrcCfg *resrc)
684 {
685    uint8_t rsrcIdx=0;
686    for(rsrcIdx=0; rsrcIdx < resrc->resrcToAddModListCount; rsrcIdx++)
687    {
688       switch(resrc->resrcToAddModList[rsrcIdx].pucchFormat)
689       {
690          case PUCCH_FORMAT_0:
691          {
692             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format0,\
693             sizeof(SchPucchFormat0));
694             break;
695          }
696          case PUCCH_FORMAT_1:
697          {
698             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format1,\
699             sizeof(SchPucchFormat1));
700             break;
701          }
702          case PUCCH_FORMAT_2:
703          {
704             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format2,\
705             sizeof(SchPucchFormat2_3));
706             break;
707          }
708          case PUCCH_FORMAT_3:
709          {
710             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format3,\
711             sizeof(SchPucchFormat2_3));
712             break;
713          }
714          case PUCCH_FORMAT_4:
715          {
716             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format4,\
717             sizeof(SchPucchFormat4));
718             break;
719          }
720       }
721    }
722 }
723
724 /*******************************************************************
725 *
726 * @brief Function to delete SCH Pdsch ServCellCfg
727 *
728 * @details
729 *
730 *    Function : deleteSchPdschServCellCfg
731 *
732 *    Functionality: Function to delete SCH Pdsch ServCellCfg
733 *
734 * @params[in] SchPdschServCellCfg *pdschServCellCfg
735 * @return void 
736 *
737 * ****************************************************************/
738
739 void deleteSchPdschServCellCfg(SchPdschServCellCfg *pdschServCellCfg)
740 {
741    SCH_FREE(pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
742    SCH_FREE(pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
743    SCH_FREE(pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
744    SCH_FREE(pdschServCellCfg->xOverhead, sizeof(SchPdschXOverhead));
745 }
746
747 /*******************************************************************
748 *
749 * @brief Function to  delete SCH UeCb
750 *
751 * @details
752 *
753 *    Function : deleteSchUeCb 
754 *
755 *    Functionality: Function to delete SCH UeCb
756 *
757 * @params[in]
758 * @return ROK     - success
759 *         RFAILED - failure
760 *
761 * ****************************************************************/
762 void deleteSchUeCb(SchUeCb *ueCb) 
763 {
764    SchPucchCfg *pucchCfg = NULLP;
765    if(ueCb)
766    {
767       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
768       if(ueCb->ueCfg.spCellCfgPres)
769       {
770          if(ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres == true)
771          {
772             pucchCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg;
773             SCH_FREE(pucchCfg->resrcSet,sizeof(SchPucchResrcSetCfg));
774             if(pucchCfg->resrc)
775             {
776                deleteSchPucchResrcCfg(pucchCfg->resrc);
777                SCH_FREE(pucchCfg->resrc, sizeof(SchPucchResrcCfg));
778             }
779             SCH_FREE(pucchCfg->format1, sizeof(SchPucchFormatCfg));
780             SCH_FREE(pucchCfg->format2, sizeof(SchPucchFormatCfg));
781             SCH_FREE(pucchCfg->format3, sizeof(SchPucchFormatCfg));
782             SCH_FREE(pucchCfg->format4, sizeof(SchPucchFormatCfg));
783             SCH_FREE(pucchCfg->schedReq, sizeof(SchPucchSchedReqCfg));
784             SCH_FREE(pucchCfg->multiCsiCfg, sizeof(SchPucchMultiCsiCfg));
785             SCH_FREE(pucchCfg->spatialInfo, sizeof(SchPucchSpatialCfg));  
786             SCH_FREE(pucchCfg->dlDataToUlAck, sizeof(SchPucchDlDataToUlAck));
787             SCH_FREE(pucchCfg->powerControl,sizeof(SchPucchPowerControl));
788          }
789          SCH_FREE(ueCb->ueCfg.spCellCfg.servCellCfg.bwpInactivityTmr, sizeof(uint8_t));
790          deleteSchPdschServCellCfg(&ueCb->ueCfg.spCellCfg.servCellCfg.pdschServCellCfg);
791       }
792       memset(ueCb, 0, sizeof(SchUeCb));
793    }
794 }
795
796 /*******************************************************************
797 *
798 * @brief Function for Ue Delete request from MAC to SCH
799 *
800 * @details
801 *
802 *    Function : MacSchUeDeleteReq 
803 *
804 *    Functionality: Function for Ue Delete request from MAC to SCH
805 *
806 * @params[in] Pst *pst, SchUeDelete  *ueDelete
807 * @return ROK     - success
808 *         RFAILED - failure
809 *
810 * ****************************************************************/
811 uint8_t MacSchUeDeleteReq(Pst *pst, SchUeDelete  *ueDelete)
812 {
813     uint8_t idx=0, ueIdx=0, ret=ROK;
814     ErrorCause result;
815     SchCellCb    *cellCb = NULLP;
816     Inst         inst = pst->dstInst - 1;
817     
818     if(!ueDelete)
819     {
820        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): Ue Delete request failed");
821        ret = RFAILED;
822     }
823     DU_LOG("\nDEBUG  -->  SCH : Ue Delete request received for crnti[%d]", ueDelete->crnti);
824     
825     cellCb = schCb[inst].cells[idx];
826
827     if(cellCb->cellId != ueDelete->cellId)
828     {
829        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): cell Id is not available");
830        result =  INVALID_CELLID;
831     }
832     else
833     {
834        GET_UE_IDX(ueDelete->crnti, ueIdx);
835        if(( cellCb->ueCb[ueIdx-1].crnti == ueDelete->crnti) && ( cellCb->ueCb[ueIdx-1].state == SCH_UE_STATE_ACTIVE))
836        {
837           deleteSchUeCb(&cellCb->ueCb[ueIdx-1]);
838           cellCb->numActvUe--;
839           result = NOT_APPLICABLE;
840        }
841        else
842        {
843           DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): SchUeCb not found");
844           result =  INVALID_UEIDX;
845        }
846     }
847     
848     if(result == NOT_APPLICABLE)
849     {
850        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_OK, result);
851     }
852     else
853     {
854        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_NOK, result);
855        ret = RFAILED;
856     }
857     return ret;
858 }
859
860 /*******************************************************************
861  *
862  * @brief Fill and send Cell delete response to MAC
863  *
864  * @details
865  *
866  *    Function :  SchSendCellDeleteRspToMac
867  *
868  *    Functionality: Fill and send Cell delete response to MAC
869  *
870  * @params[in] SchCellDelete  *ueDelete, Inst inst, SchMacRsp result
871  * @return ROK     - success
872  *         RFAILED - failure
873  *
874  * ****************************************************************/
875 uint8_t SchSendCellDeleteRspToMac(SchCellDelete  *ueDelete, Inst inst, SchMacRsp result)
876 {
877    Pst rspPst;
878    uint8_t ret=0;
879    
880    SchCellDeleteRsp  delRsp;
881
882    DU_LOG("\nINFO   --> SCH : Filling Cell Delete response");
883    memset(&delRsp, 0, sizeof(SchCellDeleteRsp));
884    delRsp.cellId = ueDelete->cellId;
885    delRsp.rsp = result;
886
887    /* Filling response post */
888    memset(&rspPst, 0, sizeof(Pst));
889    FILL_PST_SCH_TO_MAC(rspPst, inst);
890    rspPst.event = EVENT_CELL_DELETE_RSP_TO_MAC;
891    ret =  SchCellDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
892    if(ret == RFAILED)
893    {
894       DU_LOG("\nERROR  -->  SCH : SchSendCellDeleteRspToMac(): failed to send the Cell Delete response");
895       return ret;
896    }
897    return ret;
898 }
899
900 /*******************************************************************
901  *
902  * @brief Function for cellCb Deletion 
903  *
904  * @details
905  *
906  *    Function : deleteSchCellCb 
907  *
908  *    Functionality: Function for cellCb Deletion 
909  *
910  * @params[in] SchCellDelete  *cellDelete
911  * @return ROK     - success
912  *         RFAILED - failure
913  *
914  * ****************************************************************/
915 void deleteSchCellCb(SchCellCb *cellCb)
916 {
917    uint8_t idx=0;
918    if(cellCb->schDlSlotInfo)
919    {
920       for(idx=0; idx<cellCb->numSlots; idx++)
921       {
922          if(cellCb->schDlSlotInfo[idx])
923          {
924             SCH_FREE(cellCb->schDlSlotInfo[idx], sizeof(SchDlSlotInfo));
925          }
926       }
927       SCH_FREE(cellCb->schDlSlotInfo, cellCb->numSlots *sizeof(SchDlSlotInfo*));
928    }
929    if(cellCb->schUlSlotInfo)
930    {
931       for(idx=0; idx<cellCb->numSlots; idx++)
932       {
933          if(cellCb->schUlSlotInfo[idx])
934          {
935             SCH_FREE(cellCb->schUlSlotInfo[idx], sizeof(SchUlSlotInfo));  
936          }
937       }
938       SCH_FREE(cellCb->schUlSlotInfo,  cellCb->numSlots * sizeof(SchUlSlotInfo*));
939    }
940    memset(cellCb, 0, sizeof(SchCellCb));
941
942 }
943
944 /*******************************************************************
945  *
946  * @brief Function for cell Delete request from MAC to SCH
947  *
948  * @details
949  *
950  *    Function : MacSchCellDeleteReq
951  *
952  *    Functionality: Function for cell Delete request from MAC to SCH
953  *
954  * @params[in] Pst *pst, SchCellDelete  *cellDelete
955  * @return ROK     - success
956  *         RFAILED - failure
957  *
958  * ****************************************************************/
959
960 uint8_t MacSchCellDeleteReq(Pst *pst, SchCellDelete  *cellDelete)
961 {
962    uint8_t   cellIdx=0, ret = RFAILED;
963    Inst      inst = pst->dstInst - 1;
964    SchMacRsp result= RSP_OK;
965
966    if(!cellDelete)
967    {
968       DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): Ue Delete request failed");
969    }
970    else
971    {
972       GET_CELL_IDX(cellDelete->cellId, cellIdx);
973       if(schCb[inst].cells[cellIdx] == NULLP)
974       { 
975          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available", cellDelete->cellId);
976          result = RSP_NOK;
977       }
978       else
979       {
980          if(schCb[inst].cells[cellIdx]->cellId == cellDelete->cellId)
981          {
982             deleteSchCellCb(schCb[inst].cells[cellIdx]);
983             result = RSP_OK;
984             ret = ROK;
985             SCH_FREE(schCb[inst].cells[cellIdx], sizeof(SchCellCb));
986             DU_LOG("\nINFO   -->  SCH : Sending Cell Delete response to MAC");
987          }
988          else
989          {
990             DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available",cellDelete->cellId);
991             result = RSP_NOK;
992          }
993       }
994
995       if(SchSendCellDeleteRspToMac(cellDelete, inst, result)!=ROK)
996       {
997          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): failed to send Cell Delete response");
998          ret =  RFAILED;
999       }
1000
1001    }
1002    return ret;   
1003 }
1004
1005 /**********************************************************************
1006   End of file
1007  **********************************************************************/