Added Call flow debug print for all the layer
[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 #ifdef CALL_FLOW_DEBUG_LOG
362    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_ADD_UE_CONFIG_REQ_TO_SCH\n");
363 #endif
364
365    if(!ueCfg)
366    {
367       DU_LOG("\nERROR  -->  SCH :  Adding UE Config Request failed at MacSchAddUeConfigReq()");
368       return RFAILED;
369    }
370    DU_LOG("\nDEBUG  -->  SCH :  Adding UE Config Request for CRNTI[%d]", ueCfg->crnti);
371    cellCb = getSchCellCb(pst->event, inst, ueCfg);
372
373    /* Search if UE already configured */
374    GET_UE_IDX(ueCfg->crnti, ueIdx);
375    ueCb = &cellCb->ueCb[ueIdx -1];
376    if(ueCb)
377    {
378       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
379       {
380          DU_LOG("\nDEBUG  -->  SCH : CRNTI %d already configured ", ueCfg->crnti);
381          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
382          return ROK;
383       }
384    }
385    else
386    {
387       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchAddUeConfigReq() ");
388       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
389       return RFAILED;
390    }
391
392    /* Fill received Ue Configuration in UeCb */
393    memset(ueCb, 0, sizeof(SchUeCb));
394    GET_UE_IDX(ueCfg->crnti, ueIdx);
395    ueCb->ueIdx = ueIdx;
396    ueCb->crnti = ueCfg->crnti;
397    ueCb->state = SCH_UE_STATE_ACTIVE;
398    ret = fillSchUeCb(ueCb, ueCfg);
399    if(ret == ROK)
400    {
401       cellCb->numActvUe++;
402       SET_ONE_BIT(ueCb->ueIdx, cellCb->actvUeBitMap);
403       ueCb->cellCb = cellCb;
404       ueCb->srRcvd = false;
405       for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
406          ueCb->bsrInfo[lcIdx].dataVol = 0;
407
408       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
409    }
410    return ret;
411 }
412
413 /*******************************************************************
414 *
415 * @brief Fills PUSCH UL allocation
416 *
417 * @details
418 *
419 *    Function : schFillPuschAlloc
420 *
421 *    Functionality: fills PUSCH info
422 *
423 * @params[in]
424 * @return ROK     - success
425 *         RFAILED - failure
426 *
427 * ****************************************************************/
428 uint8_t schFillPuschAlloc(SchUeCb *ueCb, uint16_t pdcchSlot, uint32_t dataVol, SchPuschInfo *puschInfo)
429 {
430   uint16_t puschSlot      = 0;
431   uint16_t startRb        = 0;
432   uint8_t  numRb          = 0;
433   uint16_t tbSize         = 0;
434   uint8_t  buffer         = 5;
435   uint8_t  idx            = 0;
436   SchCellCb *cellCb       = ueCb->cellCb;
437   SchUlSlotInfo *schUlSlotInfo = NULLP;
438   uint8_t k2=0, startSymb=0 , symbLen=11;
439   
440   if(ueCb->ueCfg.spCellCfgPres == true)
441   {
442      k2 = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].k2;
443      startSymb = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].startSymbol;
444      symbLen = ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.puschCfg.timeDomRsrcAllocList[0].symbolLength;
445   }
446   puschSlot = (pdcchSlot + k2) % cellCb->numSlots;
447
448   startRb = cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb;
449   tbSize  = schCalcTbSize(dataVol + buffer); /*  2 bytes header + some buffer */
450   numRb   = schCalcNumPrb(tbSize, ueCb->ueCfg.ulModInfo.mcsIndex, symbLen);
451   /* increment PUSCH PRB */
452
453   cellCb->schUlSlotInfo[puschSlot]->puschCurrentPrb += numRb;
454
455   puschInfo->crnti             = ueCb->crnti; 
456   puschInfo->harqProcId        = SCH_HARQ_PROC_ID;
457   puschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
458   puschInfo->fdAlloc.startPrb  = startRb;
459   puschInfo->fdAlloc.numPrb    = numRb;
460   puschInfo->tdAlloc.startSymb = startSymb;
461   puschInfo->tdAlloc.numSymb   = symbLen;
462   puschInfo->tbInfo.qamOrder   = ueCb->ueCfg.ulModInfo.modOrder;
463   puschInfo->tbInfo.mcs        = ueCb->ueCfg.ulModInfo.mcsIndex;
464   puschInfo->tbInfo.mcsTable   = ueCb->ueCfg.ulModInfo.mcsTable;
465   puschInfo->tbInfo.ndi        = 1; /* new transmission */
466   puschInfo->tbInfo.rv         = 0;
467   puschInfo->tbInfo.tbSize     = tbSize;
468   puschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
469   puschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
470   puschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
471
472   /* Update pusch in cell */
473   for(idx=startSymb; idx<symbLen; idx++)
474   {
475      cellCb->schUlSlotInfo[puschSlot]->assignedPrb[idx] = startRb + numRb;
476   }
477
478   schUlSlotInfo = cellCb->schUlSlotInfo[puschSlot];
479
480   SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
481   if(!schUlSlotInfo->schPuschInfo)
482   {
483      DU_LOG("\nERROR  -->  SCH: Memory allocation failed in schAllocMsg3Pusch");
484      return RFAILED;
485   }
486   memcpy(schUlSlotInfo->schPuschInfo, puschInfo, sizeof(SchPuschInfo));
487
488   return ROK;
489 }
490
491 /*******************************************************************
492  *
493  * @brief Fills DCI for UL grant
494  *
495  * @details
496  *
497  *    Function : schFillUlDci
498  *
499  *    Functionality: fills DCI for UL grant in response to BSR
500  *
501  * @params[in]
502  * @return ROK     - success
503  *         RFAILED - failure
504  *
505  * ****************************************************************/
506 uint8_t schFillUlDci(SchUeCb *ueCb, SchPuschInfo puschInfo, DciInfo *dciInfo)
507 {
508    SchCellCb         *cellCb  = ueCb->cellCb;
509    SchControlRsrcSet coreset1 ;
510   
511    memset(&coreset1, 0, sizeof(SchControlRsrcSet));
512    if(ueCb->ueCfg.spCellCfgPres == true)
513    {
514      coreset1 = ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdcchCfg.cRSetToAddModList[0];
515    }
516    
517    dciInfo->cellId = cellCb->cellId;
518    dciInfo->crnti  = ueCb->crnti;
519
520    /* fill bwp cfg */
521    dciInfo->bwpCfg.subcarrierSpacing  = cellCb->cellCfg.sib1SchCfg.bwp.subcarrierSpacing;
522    dciInfo->bwpCfg.cyclicPrefix       = cellCb->cellCfg.sib1SchCfg.bwp.cyclicPrefix;
523    dciInfo->bwpCfg.freqAlloc.startPrb = 0;
524    dciInfo->bwpCfg.freqAlloc.numPrb   = MAX_NUM_RB; /* whole of BW */
525
526    /*fill coreset cfg */
527    //Considering number of RBs in coreset1 is same as coreset0
528    dciInfo->coresetCfg.coreSetSize      = coresetIdxTable[0][1];
529    //Considering coreset1 also starts from same symbol as coreset0
530    dciInfo->coresetCfg.startSymbolIndex = searchSpaceIdxTable[0][3];
531    dciInfo->coresetCfg.durationSymbols  = coreset1.duration;
532    memcpy(dciInfo->coresetCfg.freqDomainResource, coreset1.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
533    
534    dciInfo->coresetCfg.cceRegMappingType   = coreset1.cceRegMappingType; /* non-interleaved */
535    dciInfo->coresetCfg.regBundleSize       = 6; /* must be 6 for non-interleaved */
536    dciInfo->coresetCfg.interleaverSize     = 0; /* NA for non-interleaved */
537    dciInfo->coresetCfg.coreSetType         = 1; /* non PBCH coreset */
538    dciInfo->coresetCfg.shiftIndex          = cellCb->cellCfg.phyCellId;
539    dciInfo->coresetCfg.precoderGranularity = coreset1.precoderGranularity;
540    dciInfo->coresetCfg.cceIndex            = 0; /* 0-3 for UL and 4-7 for DL */
541    dciInfo->coresetCfg.aggregationLevel    = 4; /* same as for sib1 */
542    
543    dciInfo->formatType = FORMAT0_0;
544    
545    /* fill UL grant */
546    dciInfo->format.format0_0.resourceAllocType   = puschInfo.resAllocType;
547    dciInfo->format.format0_0.freqAlloc.startPrb  = puschInfo.fdAlloc.startPrb;
548    dciInfo->format.format0_0.freqAlloc.numPrb    = puschInfo.fdAlloc.numPrb;
549    dciInfo->format.format0_0.timeAlloc.startSymb = puschInfo.tdAlloc.startSymb;
550    dciInfo->format.format0_0.timeAlloc.numSymb   = puschInfo.tdAlloc.numSymb;
551    dciInfo->format.format0_0.rowIndex            = 0; /* row Index */
552    dciInfo->format.format0_0.mcs                 = puschInfo.tbInfo.mcs;
553    dciInfo->format.format0_0.harqProcId          = puschInfo.harqProcId;
554    dciInfo->format.format0_0.puschHopFlag        = FALSE; /* disabled */
555    dciInfo->format.format0_0.freqHopFlag         = FALSE; /* disabled */
556    dciInfo->format.format0_0.ndi                 = puschInfo.tbInfo.ndi; /* new transmission */
557    dciInfo->format.format0_0.rv                  = puschInfo.tbInfo.rv;
558    dciInfo->format.format0_0.tpcCmd              = 0; //Sphoorthi TODO: check
559    dciInfo->format.format0_0.sUlCfgd             = FALSE; /* SUL not configured */
560    
561    /* Fill DCI Structure */
562    dciInfo->dciInfo.rnti                              = ueCb->crnti;
563    dciInfo->dciInfo.scramblingId                      = cellCb->cellCfg.phyCellId;
564    dciInfo->dciInfo.scramblingRnti                    = 0;
565    dciInfo->dciInfo.cceIndex                          = 0; /* 0-3 for UL and 4-7 for DL */
566    dciInfo->dciInfo.aggregLevel                       = 4;
567    dciInfo->dciInfo.beamPdcchInfo.numPrgs             = 1;
568    dciInfo->dciInfo.beamPdcchInfo.prgSize             = 1;
569    dciInfo->dciInfo.beamPdcchInfo.digBfInterfaces     = 0;
570    dciInfo->dciInfo.beamPdcchInfo.prg[0].pmIdx        = 0;
571    dciInfo->dciInfo.beamPdcchInfo.prg[0].beamIdx[0]   = 0;
572    dciInfo->dciInfo.txPdcchPower.powerValue           = 0;
573    dciInfo->dciInfo.txPdcchPower.powerControlOffsetSS = 0;
574    dciInfo->dciInfo.pdschCfg                          = NULLP; /* No DL data being sent */
575
576    return ROK;
577 }
578
579 /*******************************************************************
580  *
581  * @brief Function to Modify Ue Config request from MAC
582  *
583  * @details
584  *
585  *    Function : MacSchModUeConfigReq
586  *
587  *    Functionality: Function to modify Ue Config request from MAC
588  *
589  * @params[in] 
590  * @return ROK     - success
591  *         RFAILED - failure
592  *
593  * ****************************************************************/
594 uint8_t MacSchModUeConfigReq(Pst *pst, SchUeCfg *ueCfg)
595 {
596    uint8_t ueIdx, lcIdx, ret = ROK;
597    SchCellCb    *cellCb = NULLP;
598    SchUeCb      *ueCb = NULLP;
599    SchUeCfgRsp  cfgRsp;
600    Inst         inst = pst->dstInst - 1;
601    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
602   
603 #ifdef CALL_FLOW_DEBUG_LOG
604    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH\n");
605 #endif
606
607    if(!ueCfg)
608    {
609       DU_LOG("\nERROR  -->  SCH : Modifying Ue Config request failed at MacSchModUeConfigReq()");
610       return RFAILED;
611    }
612    DU_LOG("\nDEBUG  -->  SCH : Modifying Ue Config Request for CRNTI[%d]", ueCfg->crnti);
613    cellCb = getSchCellCb(pst->event, inst, ueCfg);
614
615    /* Search if UE already configured */
616    GET_UE_IDX(ueCfg->crnti, ueIdx);
617    ueCb = &cellCb->ueCb[ueIdx -1];
618    
619    if(!ueCb)
620    {
621       DU_LOG("\nERROR  -->  SCH : SchUeCb not found at MacSchModUeConfigReq() ");
622       SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_NOK, &cfgRsp);
623       return RFAILED;
624    }
625    if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
626    {
627       /* Found the UeCb to Reconfig */
628       ret = fillSchUeCb(ueCb, ueCfg);
629       if(ret == ROK)
630       {
631          ueCb->cellCb = cellCb;
632          ueCb->srRcvd = false;
633          for(lcIdx=0; lcIdx<MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcIdx++)
634             ueCb->bsrInfo[lcIdx].dataVol = 0;
635
636          SchSendUeCfgRspToMac(pst->event, ueCfg, inst, RSP_OK, &cfgRsp);
637       }
638    }
639    return ret;
640 }
641
642 /*******************************************************************
643 *
644 * @brief Fill and send UE delete response to MAC
645 *
646 * @details
647 *
648 *    Function :  SchSendUeDeleteRspToMac
649 *
650 *    Functionality: Fill and send UE delete response to MAC
651 *
652 * @params[in] Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, 
653 *              ErrorCause cause
654 * @return ROK     - success
655 *         RFAILED - failure
656 *
657 * ****************************************************************/
658 void SchSendUeDeleteRspToMac(Inst inst, SchUeDelete  *ueDelete, SchMacRsp result, ErrorCause cause)
659 {
660     Pst rspPst;
661     SchUeDeleteRsp  delRsp;
662     
663     memset(&delRsp, 0, sizeof(SchUeDeleteRsp));
664     delRsp.cellId = ueDelete->cellId;
665     delRsp.crnti = ueDelete->crnti;
666     delRsp.rsp = result; 
667     delRsp.cause = cause; 
668
669     /* Filling response post */
670     memset(&rspPst, 0, sizeof(Pst));
671     FILL_PST_SCH_TO_MAC(rspPst, inst);
672     rspPst.event = EVENT_UE_DELETE_RSP_TO_MAC;
673     SchUeDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
674 }
675
676 /*******************************************************************
677 *
678 * @brief Function to delete Sch Pucch ResrcCfg
679 *
680 * @details
681 *
682 *    Function : deleteSchPucchResrcCfg 
683 *
684 *    Functionality: Function to delete Sch Pucch ResrcCfg
685 *
686 * @params[in] SchPucchResrcCfg *resrc
687 * @return void 
688 *
689 * ****************************************************************/
690
691 void deleteSchPucchResrcCfg(SchPucchResrcCfg *resrc)
692 {
693    uint8_t rsrcIdx=0;
694    for(rsrcIdx=0; rsrcIdx < resrc->resrcToAddModListCount; rsrcIdx++)
695    {
696       switch(resrc->resrcToAddModList[rsrcIdx].pucchFormat)
697       {
698          case PUCCH_FORMAT_0:
699          {
700             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format0,\
701             sizeof(SchPucchFormat0));
702             break;
703          }
704          case PUCCH_FORMAT_1:
705          {
706             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format1,\
707             sizeof(SchPucchFormat1));
708             break;
709          }
710          case PUCCH_FORMAT_2:
711          {
712             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format2,\
713             sizeof(SchPucchFormat2_3));
714             break;
715          }
716          case PUCCH_FORMAT_3:
717          {
718             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format3,\
719             sizeof(SchPucchFormat2_3));
720             break;
721          }
722          case PUCCH_FORMAT_4:
723          {
724             SCH_FREE(resrc->resrcToAddModList[rsrcIdx].SchPucchFormat.format4,\
725             sizeof(SchPucchFormat4));
726             break;
727          }
728       }
729    }
730 }
731
732 /*******************************************************************
733 *
734 * @brief Function to delete SCH Pdsch ServCellCfg
735 *
736 * @details
737 *
738 *    Function : deleteSchPdschServCellCfg
739 *
740 *    Functionality: Function to delete SCH Pdsch ServCellCfg
741 *
742 * @params[in] SchPdschServCellCfg *pdschServCellCfg
743 * @return void 
744 *
745 * ****************************************************************/
746
747 void deleteSchPdschServCellCfg(SchPdschServCellCfg *pdschServCellCfg)
748 {
749    SCH_FREE(pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
750    SCH_FREE(pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
751    SCH_FREE(pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
752    SCH_FREE(pdschServCellCfg->xOverhead, sizeof(SchPdschXOverhead));
753 }
754
755 /*******************************************************************
756 *
757 * @brief Function to  delete SCH UeCb
758 *
759 * @details
760 *
761 *    Function : deleteSchUeCb 
762 *
763 *    Functionality: Function to delete SCH UeCb
764 *
765 * @params[in]
766 * @return ROK     - success
767 *         RFAILED - failure
768 *
769 * ****************************************************************/
770 void deleteSchUeCb(SchUeCb *ueCb) 
771 {
772    uint8_t timeDomRsrcIdx;
773    SchPucchCfg *pucchCfg = NULLP;
774    SchPdschConfig *pdschCfg = NULLP;
775
776    if(ueCb)
777    {
778       SCH_FREE(ueCb->ueCfg.ambrCfg, sizeof(SchAmbrCfg));
779       if(ueCb->ueCfg.spCellCfgPres)
780       {
781          if(ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfgPres == true)
782          {
783             pdschCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfg;
784             for(timeDomRsrcIdx = 0; timeDomRsrcIdx < pdschCfg->numTimeDomRsrcAlloc; timeDomRsrcIdx++)
785                SCH_FREE(pdschCfg->timeDomRsrcAllociList[timeDomRsrcIdx].k0, sizeof(uint8_t));
786          }
787
788          if(ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfgPres == true)
789          {
790             pucchCfg = &ueCb->ueCfg.spCellCfg.servCellCfg.initUlBwp.pucchCfg;
791             SCH_FREE(pucchCfg->resrcSet,sizeof(SchPucchResrcSetCfg));
792             if(pucchCfg->resrc)
793             {
794                deleteSchPucchResrcCfg(pucchCfg->resrc);
795                SCH_FREE(pucchCfg->resrc, sizeof(SchPucchResrcCfg));
796             }
797             SCH_FREE(pucchCfg->format1, sizeof(SchPucchFormatCfg));
798             SCH_FREE(pucchCfg->format2, sizeof(SchPucchFormatCfg));
799             SCH_FREE(pucchCfg->format3, sizeof(SchPucchFormatCfg));
800             SCH_FREE(pucchCfg->format4, sizeof(SchPucchFormatCfg));
801             SCH_FREE(pucchCfg->schedReq, sizeof(SchPucchSchedReqCfg));
802             SCH_FREE(pucchCfg->multiCsiCfg, sizeof(SchPucchMultiCsiCfg));
803             SCH_FREE(pucchCfg->spatialInfo, sizeof(SchPucchSpatialCfg));  
804             SCH_FREE(pucchCfg->dlDataToUlAck, sizeof(SchPucchDlDataToUlAck));
805             SCH_FREE(pucchCfg->powerControl,sizeof(SchPucchPowerControl));
806          }
807          SCH_FREE(ueCb->ueCfg.spCellCfg.servCellCfg.bwpInactivityTmr, sizeof(uint8_t));
808          deleteSchPdschServCellCfg(&ueCb->ueCfg.spCellCfg.servCellCfg.pdschServCellCfg);
809       }
810       memset(ueCb, 0, sizeof(SchUeCb));
811    }
812 }
813
814 /*******************************************************************
815 *
816 * @brief Function for Ue Delete request from MAC to SCH
817 *
818 * @details
819 *
820 *    Function : MacSchUeDeleteReq 
821 *
822 *    Functionality: Function for Ue Delete request from MAC to SCH
823 *
824 * @params[in] Pst *pst, SchUeDelete  *ueDelete
825 * @return ROK     - success
826 *         RFAILED - failure
827 *
828 * ****************************************************************/
829 uint8_t MacSchUeDeleteReq(Pst *pst, SchUeDelete  *ueDelete)
830 {
831     uint8_t idx=0, ueIdx=0, ret=ROK;
832     ErrorCause result;
833     SchCellCb    *cellCb = NULLP;
834     Inst         inst = pst->dstInst - 1;
835    
836 #ifdef CALL_FLOW_DEBUG_LOG
837     DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_UE_DELETE_REQ_TO_SCH\n");
838 #endif
839
840     if(!ueDelete)
841     {
842        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): Ue Delete request failed");
843        ret = RFAILED;
844     }
845     DU_LOG("\nDEBUG  -->  SCH : Ue Delete request received for crnti[%d]", ueDelete->crnti);
846     
847     cellCb = schCb[inst].cells[idx];
848
849     if(cellCb->cellId != ueDelete->cellId)
850     {
851        DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): cell Id is not available");
852        result =  INVALID_CELLID;
853     }
854     else
855     {
856        GET_UE_IDX(ueDelete->crnti, ueIdx);
857        if(( cellCb->ueCb[ueIdx-1].crnti == ueDelete->crnti) && ( cellCb->ueCb[ueIdx-1].state == SCH_UE_STATE_ACTIVE))
858        {
859           deleteSchUeCb(&cellCb->ueCb[ueIdx-1]);
860           cellCb->numActvUe--;
861           result = NOT_APPLICABLE;
862        }
863        else
864        {
865           DU_LOG("\nERROR  -->  SCH : MacSchUeDeleteReq(): SchUeCb not found");
866           result =  INVALID_UEIDX;
867        }
868     }
869     
870     if(result == NOT_APPLICABLE)
871     {
872        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_OK, result);
873     }
874     else
875     {
876        SchSendUeDeleteRspToMac(inst, ueDelete, RSP_NOK, result);
877        ret = RFAILED;
878     }
879     return ret;
880 }
881
882 /*******************************************************************
883  *
884  * @brief Fill and send Cell delete response to MAC
885  *
886  * @details
887  *
888  *    Function :  SchSendCellDeleteRspToMac
889  *
890  *    Functionality: Fill and send Cell delete response to MAC
891  *
892  * @params[in] SchCellDelete  *ueDelete, Inst inst, SchMacRsp result
893  * @return ROK     - success
894  *         RFAILED - failure
895  *
896  * ****************************************************************/
897 uint8_t SchSendCellDeleteRspToMac(SchCellDelete  *ueDelete, Inst inst, SchMacRsp result)
898 {
899    Pst rspPst;
900    uint8_t ret=0;
901    
902    SchCellDeleteRsp  delRsp;
903
904    DU_LOG("\nINFO   --> SCH : Filling Cell Delete response");
905    memset(&delRsp, 0, sizeof(SchCellDeleteRsp));
906    delRsp.cellId = ueDelete->cellId;
907    delRsp.rsp = result;
908
909    /* Filling response post */
910    memset(&rspPst, 0, sizeof(Pst));
911    FILL_PST_SCH_TO_MAC(rspPst, inst);
912    rspPst.event = EVENT_CELL_DELETE_RSP_TO_MAC;
913    ret =  SchCellDeleteRspOpts[rspPst.selector](&rspPst, &delRsp);
914    if(ret == RFAILED)
915    {
916       DU_LOG("\nERROR  -->  SCH : SchSendCellDeleteRspToMac(): failed to send the Cell Delete response");
917       return ret;
918    }
919    return ret;
920 }
921
922 /*******************************************************************
923  *
924  * @brief Function for cellCb Deletion 
925  *
926  * @details
927  *
928  *    Function : deleteSchCellCb 
929  *
930  *    Functionality: Function for cellCb Deletion 
931  *
932  * @params[in] SchCellDelete  *cellDelete
933  * @return ROK     - success
934  *         RFAILED - failure
935  *
936  * ****************************************************************/
937 void deleteSchCellCb(SchCellCb *cellCb)
938 {
939    uint8_t idx=0;
940    if(cellCb->schDlSlotInfo)
941    {
942       for(idx=0; idx<cellCb->numSlots; idx++)
943       {
944          if(cellCb->schDlSlotInfo[idx])
945          {
946             SCH_FREE(cellCb->schDlSlotInfo[idx], sizeof(SchDlSlotInfo));
947          }
948       }
949       SCH_FREE(cellCb->schDlSlotInfo, cellCb->numSlots *sizeof(SchDlSlotInfo*));
950    }
951    if(cellCb->schUlSlotInfo)
952    {
953       for(idx=0; idx<cellCb->numSlots; idx++)
954       {
955          if(cellCb->schUlSlotInfo[idx])
956          {
957             SCH_FREE(cellCb->schUlSlotInfo[idx], sizeof(SchUlSlotInfo));  
958          }
959       }
960       SCH_FREE(cellCb->schUlSlotInfo,  cellCb->numSlots * sizeof(SchUlSlotInfo*));
961    }
962    memset(cellCb, 0, sizeof(SchCellCb));
963
964 }
965
966 /*******************************************************************
967  *
968  * @brief Function for cell Delete request from MAC to SCH
969  *
970  * @details
971  *
972  *    Function : MacSchCellDeleteReq
973  *
974  *    Functionality: Function for cell Delete request from MAC to SCH
975  *
976  * @params[in] Pst *pst, SchCellDelete  *cellDelete
977  * @return ROK     - success
978  *         RFAILED - failure
979  *
980  * ****************************************************************/
981
982 uint8_t MacSchCellDeleteReq(Pst *pst, SchCellDelete  *cellDelete)
983 {
984    uint8_t   cellIdx=0, ret = RFAILED;
985    Inst      inst = pst->dstInst - 1;
986    SchMacRsp result= RSP_OK;
987    
988 #ifdef CALL_FLOW_DEBUG_LOG
989    DU_LOG("\nCall Flow: ENTMAC -> ENTSCH : EVENT_CELL_DELETE_REQ_TO_SCH\n");
990 #endif   
991
992    if(!cellDelete)
993    {
994       DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): Ue Delete request failed");
995    }
996    else
997    {
998       GET_CELL_IDX(cellDelete->cellId, cellIdx);
999       if(schCb[inst].cells[cellIdx] == NULLP)
1000       { 
1001          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available", cellDelete->cellId);
1002          result = RSP_NOK;
1003       }
1004       else
1005       {
1006          if(schCb[inst].cells[cellIdx]->cellId == cellDelete->cellId)
1007          {
1008             deleteSchCellCb(schCb[inst].cells[cellIdx]);
1009             result = RSP_OK;
1010             ret = ROK;
1011             SCH_FREE(schCb[inst].cells[cellIdx], sizeof(SchCellCb));
1012             DU_LOG("\nINFO   -->  SCH : Sending Cell Delete response to MAC");
1013          }
1014          else
1015          {
1016             DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): cell Id[%d] is not available",cellDelete->cellId);
1017             result = RSP_NOK;
1018          }
1019       }
1020
1021       if(SchSendCellDeleteRspToMac(cellDelete, inst, result)!=ROK)
1022       {
1023          DU_LOG("\nERROR  -->  SCH : MacSchCellDeleteReq(): failed to send Cell Delete response");
1024          ret =  RFAILED;
1025       }
1026
1027    }
1028    return ret;   
1029 }
1030
1031 /**********************************************************************
1032   End of file
1033  **********************************************************************/